'this' argument discards qualifiers

I'm trying to create a class to control a servo.

#include <Servo.h>

class ServoInfo {
    byte min;
    byte max;
    byte pin;

    ServoInfo(byte min, byte max, byte pin) {
      min = min;
      max = max;
      pin = pin;
    void write(byte position) {

    Servo servo;

const ServoInfo SERVOS[] {
  ServoInfo(25, 105, 9)

void setup() {

void loop() { ; }

The problem I have that it doesn't work (or at least gives me compilation warnings):

C:\Users\banks\Documents\Arduino\RobotArm\RobotArm.ino: In function 'void setup()':
C:\Users\banks\Documents\Arduino\RobotArm\RobotArm.ino:29:21: warning: passing 'const ServoInfo' as 'this' argument discards qualifiers [-fpermissive]
C:\Users\banks\Documents\Arduino\RobotArm\RobotArm.ino:16:10: note:   in call to 'void ServoInfo::write(byte)'
     void write(byte position) {
Sketch uses 1746 bytes (5%) of program storage space. Maximum is 32256 bytes.
Global variables use 53 bytes (2%) of dynamic memory, leaving 1995 bytes for local variables. Maximum is 2048 bytes.

Compilation complete.

What is this error trying to tell me? I get the feeling that is something to do with the . ,-> or :: but I don't know what. I'm a C# developer, so to me everything is a ..

you made array of const servos and now you want to modify const object. get rid of const in array definition

Wow. That worked.

I'm not modifying though, just invoking a function. Why would the const prevent/affect that?

the function is not const or modifying a member

Which arduino ide are you using? This compiles fine for me (no warnings) in 1.8.15.

Version: 2.0.0-beta.12
Date: 2021-10-07T07:55:54.325Z (1 month ago)
CLI Version: 0.19.1 alpha [718bbbf2]

Yeah, never mind. I didn't have compiler warnings enabled. Thanks anyways, I think I should update my IDE looking at your version.

Bare in mind that it's the 2.0 beta. There isn't a RTM version yet.

1 Like

Adding to killzone_kid's answer:
From the compilers point of view you are modifying the Servo object inside your ServoInfo class. Because the compiler couldn't possibly know, that Servo::write does not modify the Servo object.

You would be able to do that, if the writers of the Servo Library would've made the write function const. However they might keep track of the position internally.

1 Like

That make sense then. Thanks.

If you want to assure, that min,max and pin within the class cannot be changed, you must declare them as const and initialize them within the constructor via a initializer list.
And it is common, to start all private variables with a '_'.
'min' and 'max' are also macros in the IDE, which might lead to problems in the initializer list.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.