C++ 丢弃限定符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21189997/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ discards qualifiers
提问by Anthony
I have this error:
我有这个错误:
BSPArduino.cpp:316: error: passing 'const BSPArduino' as 'this' argument of 'virtual void BSPArduino::enableWdt(const WATCHDOG_TIMER_DELAY&, const ___bool&)' discards qualifiers
BSPArduino.cpp:316: 错误:将“const BSPArduino”作为“virtual void BSPArduino::enableWdt(const WATCHDOG_TIMER_DELAY&, const ___bool&)”的“this”参数传递会丢弃限定符
This method is define like that:
这个方法是这样定义的:
void BSPArduino::enableWdt(const WATCHDOG_TIMER_DELAY &delay, const ___bool &enable)
I want to call it like that:
我想这样称呼它:
enableWdt(this->watchdogTimer, ___false);
With:
和:
WATCHDOG_TIMER_DELAY watchdogTimer;
I don't understand why this build error...
我不明白为什么这个构建错误......
Thank you so much for your help
非常感谢你的帮助
Anthony
安东尼
回答by Bids
BSPArduino::enableWdt() is a non-const method. If you try and call a non-const method from a const one you will get this error.
BSPArduino::enableWdt() 是一种非常量方法。如果您尝试从 const 调用非常量方法,则会收到此错误。
Essentially the error is trying to tell you that you are discarding the constness of "this".
本质上,错误是试图告诉您您正在丢弃“this”的常量性。
回答by Mike Seymour
You're trying to call a non-const
function from a const
member function; that's not allowed.
您试图const
从const
成员函数调用非函数;这是不允许的。
If possible, add a const
qualifier to enableWdt
. If that's not possible (because it modifies the object) then you'll have to either remove the const
qualifier from the calling function, or restructure the code so that enableWdt
is called from somewhere else.
如果可能,请将const
限定符添加到enableWdt
. 如果这是不可能的(因为它修改了对象),那么您必须const
从调用函数中删除限定符,或者重构代码以便enableWdt
从其他地方调用。