C++丢弃限定符

C++ discards qualifiers

本文关键字:C++      更新时间:2023-10-16

我有这个错误:

BSPArduino.cpp:316:错误:将"const BSPArduino"传递为"this"'virtual void BSPArduino::enableWdt(constWATCHDOG_TIMER_DELAY&, 常量 ___bool&)' 丢弃限定符

此方法定义如下:

void BSPArduino::enableWdt(const WATCHDOG_TIMER_DELAY &delay, const ___bool &enable)

我想这样称呼它:

enableWdt(this->watchdogTimer, ___false);

跟:

WATCHDOG_TIMER_DELAY watchdogTimer;

为什么会出现此构建错误?

BSPArduino::enableWdt() 是一个非常量方法。如果您尝试从 const 方法调用非 const 方法,则会收到此错误。

从本质上讲,错误是试图告诉你你正在丢弃"这个"的恒定性。

您正在尝试从const成员函数调用非const函数;这是不允许的。

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

相关文章:
  • 没有找到相关文章