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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 23:31:41  来源:igfitidea点击:

C++ discards qualifiers

c++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-constfunction from a constmember function; that's not allowed.

您试图constconst成员函数调用非函数;这是不允许的。

If possible, add a constqualifier to enableWdt. If that's not possible (because it modifies the object) then you'll have to either remove the constqualifier from the calling function, or restructure the code so that enableWdtis called from somewhere else.

如果可能,请将const限定符添加到enableWdt. 如果这是不可能的(因为它修改了对象),那么您必须const从调用函数中删除限定符,或者重构代码以便enableWdt从其他地方调用。