g++: const 丢弃限定符

发布于 2024-08-24 21:27:21 字数 1056 浏览 3 评论 0原文

为什么我会收到 discard qualifiers 错误:

customExc.cpp: In member function ‘virtual const char* CustomException::what() const’:
customExc.cpp: error: passing ‘const CustomException’ as ‘this’ argument of ‘char customException::code()’ discards qualifiers

在下面的代码示例中,

#include <iostream>


class CustomException: public std::exception {

public:

    virtual const char* what() const throw() {
        static std::string msg;
        msg  = "Error: ";
        msg += code();  // <---------- this is the line with the compile error 
        return msg.c_str();
    }

    char code() { return 'F'; }
};

我在解决类似问题之前已经在 SOF 上进行了搜索。

我已经在每个可能的地方添加了一个 const

请启发我 - 我不明白这一点...

编辑: 以下是在 Ubuntu-Carmic-32bit (g++ v4.4.1) 上重现的步骤,

  1. 将示例保存为 customExc.cpp
  2. 类型 make customExc.o

编辑:该错误与CustomException有关。类 Foo 与它无关。所以我已经删除了它。

why do I get a discard qualifiers error:

customExc.cpp: In member function ‘virtual const char* CustomException::what() const’:
customExc.cpp: error: passing ‘const CustomException’ as ‘this’ argument of ‘char customException::code()’ discards qualifiers

on the following code example

#include <iostream>


class CustomException: public std::exception {

public:

    virtual const char* what() const throw() {
        static std::string msg;
        msg  = "Error: ";
        msg += code();  // <---------- this is the line with the compile error 
        return msg.c_str();
    }

    char code() { return 'F'; }
};

I have searched around on SOF before regarding simular issues.

I have already added a const on every possible place.

Please enlighten me - I don't get the point...

EDIT:
here are the steps to reproduce on Ubuntu-Carmic-32bit (g++ v4.4.1)

  1. save example as customExc.cpp
  2. type make customExc.o

EDIT: The error is related to CustomException. The class Foo has nothing to do with it. So I have deleted it.

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

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

发布评论

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

评论(4

时常饿 2024-08-31 21:27:21

CustomException::what 调用 CustomException::codeCustomException::what 是一个 const 方法,由 const after what() 表示。由于它是一个 const 方法,因此它不能执行任何可能修改自身的操作。 CustomException::code 不是 const 方法,这意味着它承诺不修改自身。因此 CustomException::what 无法调用 CustomException::code

请注意,const 方法不一定与 const 实例相关。 Foo::bar 可以将其 exc 变量声明为非 const 并调用 const 方法,例如 CustomException::what;这仅仅意味着 CustomException::what 承诺不会修改 exc,但其他代码可能会。

C++ 常见问题解答提供了有关 const 方法。

CustomException::what calls CustomException::code. CustomException::what is a const method, as signified by the const after what(). Since it is a const method, it cannot do anything that may modify itself. CustomException::code is not a const method, which means that it does not promise to not modify itself. So CustomException::what can't call CustomException::code.

Note that const methods are not necessarily related to const instances. Foo::bar can declare its exc variable as non-const and call const methods like CustomException::what; this simply means that CustomException::what promises not to modify exc, but other code might.

The C++ FAQ has a bit more information on const methods.

水晶透心 2024-08-31 21:27:21
   int code() const { return 42; }
   int code() const { return 42; }
不疑不惑不回忆 2024-08-31 21:27:21

您的 what() 是 const 成员函数,但 code() 不是。

只需将 code() 更改为 code() const 即可。

Your what() is a const member function, but code() is not.

Just change code() to code() const.

戈亓 2024-08-31 21:27:21

您的 code() 成员函数未声明为 const。从 const 成员函数调用非常量成员函数(在本例中为 what())是非法的。

将您的 code() 成员设置为常量。

Your code() member function is not declared const. Calling non-const member functions from const member functions (what() in this case) is illegal.

Make your code() member const.

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