I am trying to pass in the memory address of the mpuTest
class property to the fsVar.initialize()
method, but I get this error:
main.cpp:38:42: error: taking address of rvalue [-fpermissive]
38 | fsVar.initialize(&test3Var.getMpuTest());
I have done a little reading and have learned that an rvalue is something that may not have storage or memory associated with it, like a simple integer. I also know that an lvalue is something that is stored in memory with an address, like a variable.
But I have noticed with the below code that if I have a getter in a class for a property of that class, and I try and use that return value's memory address with &
, it will give me the above error. If you don't understand this example please view the below code example.
#include <iostream>
using namespace std;
class mpu {
public:
void printSomething() {
cout << "Hi" << endl;
}
};
class fs {
private:
mpu* t1;
public:
void initialize(mpu *mpuPtr) {
this->t1 = mpuPtr;
}
void printSomething2() {
this->t1->printSomething();
}
};
class Test3 {
private:
mpu mpuTest;
public:
mpu getMpuTest() {
return mpuTest;
}
};
int main()
{
Test3 test3Var;
fs fsVar;
fsVar.initialize(&test3Var.getMpuTest());
fsVar.printSomething2();
return 0;
}