挥发性的+ C++ 中不允许对象组合?

发布于 2024-09-19 22:43:33 字数 748 浏览 6 评论 0原文

我正在使用 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

当我注释掉下面的 initWontWork() 函数时,该错误消失。错误告诉我什么以及如何解决它,而不必求助于使用在 易失性结构 上运行的 static 函数?

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;

I'm using an embedded compiler for the TI TMS320F28335, so I'm not sure if this is a general C++ problem (don't have a C++ compiler running on hand) or just my compiler. Putting the following code snippet in my code gives me a compile error:

"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

The error goes away when I comment out the initWontWork() function below. What is the error telling me and how can I get around it without having to resort to using static functions that operate on a 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;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迷雾森÷林ヴ 2024-09-26 22:43:33

同样,你不能这样做:

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; }

In the same way you cannot do this:

struct foo
{
    void bar();
};

const foo f;
f.bar(); // error, non-const function with const object

You cannot do this:

struct baz
{
    void qax();
};

volatile baz g;
g.qax(); // error, non-volatile function with volatile object

You must cv-qualify the functions:

struct foo
{
    void bar() const;
};

struct baz
{
    void qax() volatile;
};

const foo f;
f.bar(); // okay

volatile baz g;
g.qax(); // okay

So for you:

void reset() volatile { x = 0; }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文