gpt4 book ai didi

c++ - 为什么 Visual Studio 2010 调试器看不到静态 const 类成员?

转载 作者:可可西里 更新时间:2023-11-01 17:37:09 30 4
gpt4 key购买 nike

此问题与随后提出的问题密切相关here.

定义类内常量的方法描述here通过 Stroustrup。

当我遵循 Stroustrup 的方法时,我看到了预期的结果。但是,在 Visual Studio 2010 中,调试器无法解析该类范围内的 static const 类成员。这就是我的意思:

#include <iostream>

class Foo {
public:
static const int A = 50;
char arr[A];
void showA();
};

void Foo::showA() {
std::cout << "showA = " << A << "\n";
}

int main() {
Foo f;
f.showA();
}

当调试器在 showA() 中时,“watch”窗口报告:

Error: Symbol "Foo::A" not found

我想强调的是,该程序确实按预期运行,即输出为:

showA = 50

程序返回 0。

其他人能否使用 Visual Studio 2010 重现此内容?这是调试器中的错误吗?

最佳答案

您可以在全局命名空间范围内为您的静态数据成员添加一个定义:

const int Foo::A;

添加静态数据成员定义,这不是必需但允许的,似乎可以解决您的问题。

我在 VS2010 的调试版本上对此进行了测试,当定义存在时,A 的值在调试窗口中正确显示(而当定义缺失时会报告错误消息,与你提到的一致)。

关于c++ - 为什么 Visual Studio 2010 调试器看不到静态 const 类成员?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16365826/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com