C++ discards qualifiers
Asked Answered
C

2

5

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

This method is define like this:

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

I want to call it like this:

enableWdt(this->watchdogTimer, ___false);

With:

WATCHDOG_TIMER_DELAY watchdogTimer;

Why do I have this build error?

Cutshall answered 17/1, 2014 at 15:46 Comment(2)
The calling context is const (eg void f() const { ... } )Leslileslie
You're calling a non-const method with an object that's const.Kyd
S
17

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.

Essentially the error is trying to tell you that you are discarding the constness of "this".

Stays answered 17/1, 2014 at 15:51 Comment(1)
The word of the day is... constness! Often used when describing adherence to const correctness.Metathesize
G
4

You're trying to call a non-const function from a const member function; that's not allowed.

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.

Grivet answered 17/1, 2014 at 15:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.