C++丢弃限定符

Ant*_*ony 4 c++ qualifiers

我有这个错误:

BSPArduino.cpp:316:错误:将'const BSPArduino'作为'this'参数传递给'virtual void BSPArduino :: enableWdt(const WATCHDOG_TIMER_DELAY&,const ___ bool&)'丢弃限定符

这个方法定义如下:

void BSPArduino::enableWdt(const WATCHDOG_TIMER_DELAY &delay, const ___bool &enable)
Run Code Online (Sandbox Code Playgroud)

我想这样称呼它:

enableWdt(this->watchdogTimer, ___false);
Run Code Online (Sandbox Code Playgroud)

附:

WATCHDOG_TIMER_DELAY watchdogTimer;
Run Code Online (Sandbox Code Playgroud)

我不明白为什么这个构建错误...

非常感谢你的帮助

安东尼

Bid*_*ids 15

BSPArduino :: enableWdt()是一种非const方法.如果您尝试从const中调用非const方法,则会出现此错误.

本质上,错误是试图告诉你,你正在丢弃"这个"的常量.

  • 这一天的话是......不变!在描述遵守[const correctness]时经常使用(https://isocpp.org/wiki/faq/const-correctness#overview-const). (2认同)

Mik*_*our 4

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

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