g++: const 丢弃限定符
为什么我会收到 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) 上重现的步骤,
- 将示例保存为
customExc.cpp
- 类型
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)
- save example as
customExc.cpp
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
CustomException::what
调用CustomException::code
。CustomException::what
是一个 const 方法,由 const afterwhat()
表示。由于它是一个 const 方法,因此它不能执行任何可能修改自身的操作。CustomException::code
不是 const 方法,这意味着它不承诺不修改自身。因此CustomException::what
无法调用CustomException::code
。请注意,const 方法不一定与 const 实例相关。
Foo::bar
可以将其exc
变量声明为非 const 并调用 const 方法,例如CustomException::what
;这仅仅意味着CustomException::what
承诺不会修改exc
,但其他代码可能会。C++ 常见问题解答提供了有关 const 方法。
CustomException::what
callsCustomException::code
.CustomException::what
is a const method, as signified by the const afterwhat()
. 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. SoCustomException::what
can't callCustomException::code
.Note that const methods are not necessarily related to const instances.
Foo::bar
can declare itsexc
variable as non-const and call const methods likeCustomException::what
; this simply means thatCustomException::what
promises not to modifyexc
, but other code might.The C++ FAQ has a bit more information on const methods.
您的
what()
是 const 成员函数,但code()
不是。只需将
code()
更改为code() const
即可。Your
what()
is a const member function, butcode()
is not.Just change
code()
tocode() const
.您的
code()
成员函数未声明为const
。从 const 成员函数调用非常量成员函数(在本例中为what()
)是非法的。将您的
code()
成员设置为常量。Your
code()
member function is not declaredconst
. Calling non-const member functions from const member functions (what()
in this case) is illegal.Make your
code()
member const.