鱼C论坛

 找回密码
 立即注册
查看: 1088|回复: 5

Visual Studio C++ 代码高亮部分失效

[复制链接]
发表于 2023-10-22 21:43:41 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
我在编程的时候突然遇到了这个问题:
代码区域.png
注意到,在第85行处,代码的自动高亮消失了一部分,但是 Intellisense 无报错和警告,我很不能理解,这是为什么?怎么解决?
以下是我这个文件的完整代码:
#pragma once

namespace shape
{
        template <typename number = int>
        class Dot
        {
                public:
                        number x = 0;
                        number y = 0;
                        Dot() = default;
                        Dot(number x, number y)
                        {
                                this->x = x;
                                this->y = y;
                        };
        };

        template <typename number = int>
        class Rect
        {
                public:
                        Dot<number> position;
                        number width = 0;
                        number height = 0;
                        Rect() = default;
                        Rect(const number width, const number height, const Dot<number> position = {})
                        {
                                this->width = width;
                                this->height = height;
                                this->position = position;
                                this->no_negative_size();
                        };
                        Rect(const Dot<number> from, const Dot<number> to = from)
                        {
                                this->position = from;
                                this->width = to.x - from.x;
                                this->height = to.y - from.y;
                                this->no_negative_size();
                        };
                        inline Dot<number> get_absolute(Dot<number> relative)
                        {
                                return Dot<number>(relative.x + this->position.x, relative.y + this->position.y);
                        };
                        Dot<number> get_absolute(const choice::relative_pos relative) const
                        {
                                if (!((relative & choice::relative_pos::Center) && (relative & choice::relative_pos::Middle)))
                                {
                                        throw error::ChoiceError();
                                };
                                Dot<number> result;
                                if (relative & choice::relative_pos::Left)
                                {
                                        result.x += this->position.x;
                                };
                                if (relative & choice::relative_pos::Right)
                                {
                                        result.x += this->position.x + this->width;
                                };
                                if (relative & choice::relative_pos::Top)
                                {
                                        result.y += this->position.y;
                                };
                                if (relative & choice::relative_pos::Bottom)
                                {
                                        result.y += this->position.y + this->height;
                                };
                                if (relative & choice::relative_pos::Center) // Includes Left & Right
                                {
                                        result.x /= 2;
                                };
                                if (relative & choice::relative_pos::Middle) // Includes Top & Bottom
                                {
                                        result.y /= 2;
                                };
                                return result;
                        };
                        inline void set_absolute(const Dot<number> relative, const Dot<number> absolute)
                        {
                                this->position.x = absolute.x - relative.x;
                                this->position.y = absolute.y - relative.y;
                        };
                        inline void set_absolute(const choice::relative_pos relative, Dot<number> absolute)
                        {
                                this->set_absolute(this->get_absolute(relative), absolute);
                        };
                protected:
                        inline void no_negative_size()
                        {
                                if (this->width < 0)
                                {
                                        this->width = 0;
                                };
                                if (this->height < 0)
                                {
                                        this->height = 0;
                                };
                        };
        };
};
其中引用到了"choice"和"error"命名空间,其来源分别在这里:
namespace choice
{
        enum relative_pos
        {
                Left   = 0b0001,
                Right  = 0b0010,
                Top    = 0b0100,
                Bottom = 0b1000,
                Center = Left | Right,
                Middle = Top | Bottom
        };
};
namespace error
{
        class BasicError : public std::exception
        {
                public:
                        std::string reason;
                        BasicError() = default;
                        BasicError(std::string reason)
                        {
                                this->reason = reason;
                        };
        };
        class ChoiceError : public BasicError {};
};
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-10-22 21:43:56 | 显示全部楼层
这个问题可能是因为Visual Studio的代码高亮器没有正确解析模板类的语法。在这种情况下,您可以尝试使用以下解决方法:

1. 确保您的Visual Studio版本是最新的,并且已经安装了所有更新。

2. 尝试在模板类名称后添加空格,例如: Dot<number> position;  改为  Dot<number> position ; 。

3. 尝试使用全局命名空间限定符,例如: ::shape::Dot<number> position; 。

4. 尝试使用 typename 关键字,例如: typename Dot<number>::position; 。

如果以上方法都没有解决问题,您可以尝试使用另一个代码高亮器插件,例如Visual Assist X。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-10-22 21:44:22 | 显示全部楼层
根据您给出的代码,我注意到在第85行处确实出现了代码高亮失效的情况。这可能是由于以下原因之一造成的:

1. 编辑器配置问题:检查您的编辑器设置,确保已启用适当的代码高亮功能。有时,编辑器可能会出现错误或配置问题,导致代码高亮部分失效。

2. 语法错误:请注意,在第85行之前是否存在任何语法错误。语法错误可能会导致编辑器无法正确解析代码,并导致代码高亮失效。

为了解决这个问题,您可以尝试以下几种方法:

1. 清除编辑器缓存:有时,编辑器缓存可能会导致问题。您可以尝试清除编辑器的缓存并重新加载代码文件,看看是否能够解决代码高亮失效的问题。

2. 关闭和重新打开文件:尝试关闭当前打开的文件,并重新打开它。有时,重新加载文件可以修复代码高亮的问题。

3. 检查语法错误:仔细检查第85行之前的代码,确保没有语法错误。如果有任何错误,及时修复它们。

4. 更新编辑器:如果您使用的是旧版本的编辑器,请尝试升级到最新版本。新版本的编辑器通常修复了许多问题和错误。

如果以上方法都无法解决问题,您可以尝试在其他编辑器中打开同样的代码文件,查看是否仍然存在代码高亮失效的问题。如果在其他编辑器中也出现了相同的问题,那么可能是代码本身的问题。

希望这些方法能够帮助您解决代码高亮失效的问题!如果问题仍然存在,请提供更多细节或尝试提供更小的代码示例,以便我可以更好地帮助您解决问题。

球一个最佳答案谢谢啦!这对我非常重要!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-10-22 21:49:19 | 显示全部楼层
vs bug 了吧
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-10-22 23:06:44 | 显示全部楼层
好像 Python 的 IDLE 也有类似的bug?@cjjJasonchen 说的

https://fishc.com.cn/thread-233186-1-1.html
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-10-23 07:10:33 | 显示全部楼层
歌者文明清理员 发表于 2023-10-22 23:06
好像 Python 的 IDLE 也有类似的bug?@cjjJasonchen 说的

https://fishc.com.cn/thread-233186-1-1.html

idle只要退出重进就好了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-10-5 19:14

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表