挥发性+;C+中不允许对象组合+;? 我使用的是TI TMS32 F2335的嵌入式编译器,所以我不确定这是否是一个通用的C++问题(没有一个C++编译器在手边运行)或者只是我的编译器。将以下代码段放入我的代码中会导致编译错误: "build\main.cpp", line 61: error #317: the object has cv-qualifiers that are not compatible with the member function object type is: volatile Foo::Bar
当我注释掉下面的挥发性+;C+中不允许对象组合+;? 我使用的是TI TMS32 F2335的嵌入式编译器,所以我不确定这是否是一个通用的C++问题(没有一个C++编译器在手边运行)或者只是我的编译器。将以下代码段放入我的代码中会导致编译错误: "build\main.cpp", line 61: error #317: the object has cv-qualifiers that are not compatible with the member function object type is: volatile Foo::Bar,c++,compiler-construction,volatile,C++,Compiler Construction,Volatile,当我注释掉下面的initWontWork()函数时,错误就消失了。错误告诉了我什么?我如何避免它而不必使用在volatile结构上运行的静态函数 struct Foo { struct Bar { int x; void reset() { x = 0; } static void doReset(volatile Bar& bar) { bar.x = 0; } } bar; volatile Bar&a
initWontWork()
函数时,错误就消失了。错误告诉了我什么?我如何避免它而不必使用在volatile结构上运行的静态
函数
struct Foo
{
struct Bar
{
int x;
void reset() { x = 0; }
static void doReset(volatile Bar& bar) { bar.x = 0; }
} bar;
volatile Bar& getBar() { return bar; }
//void initWontWork() { getBar().reset(); }
void init() { Bar::doReset(getBar()); }
} foo;
同样,您也不能这样做:
struct foo
{
void bar();
};
const foo f;
f.bar(); // error, non-const function with const object
struct baz
{
void qax();
};
volatile baz g;
g.qax(); // error, non-volatile function with volatile object
您不能这样做:
struct foo
{
void bar();
};
const foo f;
f.bar(); // error, non-const function with const object
struct baz
{
void qax();
};
volatile baz g;
g.qax(); // error, non-volatile function with volatile object
您必须确认以下功能:
struct foo
{
void bar() const;
};
struct baz
{
void qax() volatile;
};
const foo f;
f.bar(); // okay
volatile baz g;
g.qax(); // okay
所以对你来说:
void reset() volatile { x = 0; }
啊哈——谢谢!我从未见过“volatile”这样的用法。谢谢,因为它解决了这个问题。但是,我认为如果你能解释为什么会出现错误,答案会更好:“同样,你也不能这样做:cv限定符表示常量或volatile…这暗示你应该已经知道一半答案,只需更改关键字即可。”