2

I am trying to create a class that will handle events. class2 represents events, class1 is a structure where events are stored and handled. Here is my simplified code to prove the concept, it doesn't work:

class TestClass2{
  public:
    TestClass2(int x,int y,int z) {
      _x=x;
      _y=y;
      _z=z;
    }
  private:
    int _x,_y,_z;
};

class TestClass{
  public:
    void putVal(TestClass2 x)volatile{
      // The error is happening here.
      test=x;

      return;
    }
  private:
    TestClass2 test;
};

volatile TestClass testObj = TestClass();

IRAM_ATTR void testISR(){
  TestClass2 testInput = TestClass2(1,2,3);
  testObj.putVal(testInput);
}

uint8_t EventPin = 5; 
void setup() {
  Serial.begin(115200);
  pinMode(EventPin, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(EventPin), testISR, CHANGE);
}

void loop() {
  // put your main code here, to run repeatedly:
  Serial.println(millis());
  delay(1000);
}

I am getting this error:

Compilation error: passing 'volatile TestClass2' as 'this' argument discards qualifiers \[-fpermissive\]

If I replace the array of TestClass2 with an array of integers, it works.

Where am I making a mistake?

user4581301
  • 33,082
  • 7
  • 33
  • 54
boiledRice
  • 31
  • 4
  • I simplified code so it shows the problem better, got rid of arrays, now its just class containing instance of other class. error is the same, line is commented – boiledRice May 08 '23 at 16:31
  • So now if you replace TestClass 2 with int test and change the argument of the function correspondingly, it works, right? – Nick S. May 08 '23 at 17:29
  • I'm going to assume that what you have works with an int, because int is probably volatile-qualified, but your TestClass2 object is not. What if you instantiate the TestClass2 testInput as volatile, what happens then? – Nick S. May 08 '23 at 17:49
  • same error, but now testObj.putVal(testInput); is also highlighted – boiledRice May 08 '23 at 18:03
  • If i initialize test input inside putVal() method and make it volatile, error is Compilation error: ambiguous overload for 'operator=' (operand types are 'volatile TestClass2' and 'volatile TestClass2') – boiledRice May 08 '23 at 18:18

2 Answers2

1

You need to make the functions within the TestClass volotile as well.

If you would've looked up your error there, you'd find this, which would answer your question.

Juraj
  • 3,490
  • 4
  • 18
  • 25
Nick S.
  • 168
  • 1
  • 12
  • 1
    You are right, but the only method which is being called is already volatile. As i written, if i replace TestClass2 array with int array, everything works fine, so problem is somewhere else. Thanks for tip – boiledRice May 08 '23 at 16:12
  • 1
    And from stack exchange they just sent me here :) And they are right, problem is not Ardiuno specific, it is more about c++ – boiledRice May 08 '23 at 16:33
  • @boiledRice ah, snap, just checked the edit history - you did have the putVal as a volatile from beginning. For some reason I overlooked that, sorry! And fair. Okay, letsee. – Nick S. May 08 '23 at 17:28
1

I did not found a solution so far. For now, i eliminate this problem by using struct to store data inside volatile instance.

struct TestStruct{
  int x;
  int y;
  int z;
};

class TestClass{
  public:
    TestClass(){
      // test = TestClass2();
    }
    void putVal(int x, int y, int z)volatile{
      test.x = x;
      test.y = y;
      test.z = z;
      return;
    }
  private:
    TestStruct test;
};
boiledRice
  • 31
  • 4
  • 1
    That's fair; for your original solution--the 2 classes--to work, it seems that you'd have to properly define the copy-ctor, the assignment operator, and other ctors/operators that relate to moving data around for the volatile class instances. – Nick S. May 08 '23 at 19:55