Jas*_*n S 4 c++ compiler-construction volatile
我正在为TI TMS320F28335使用嵌入式编译器,因此我不确定这是否是一般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
Run Code Online (Sandbox Code Playgroud)
当我注释掉initWontWork()
下面的函数时,错误消失了.有什么错误告诉我,如何在不必使用static
操作函数的情况下绕过它volatile struct
?
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;
Run Code Online (Sandbox Code Playgroud)
GMa*_*ckG 11
以同样的方式你不能这样做:
struct foo
{
void bar();
};
const foo f;
f.bar(); // error, non-const function with const object
Run Code Online (Sandbox Code Playgroud)
你不能做这个:
struct baz
{
void qax();
};
volatile baz g;
g.qax(); // error, non-volatile function with volatile object
Run Code Online (Sandbox Code Playgroud)
你必须对这些功能进行cv认证:
struct foo
{
void bar() const;
};
struct baz
{
void qax() volatile;
};
const foo f;
f.bar(); // okay
volatile baz g;
g.qax(); // okay
Run Code Online (Sandbox Code Playgroud)
所以对你来说:
void reset() volatile { x = 0; }
Run Code Online (Sandbox Code Playgroud)