Search code examples
c++pointersoperator-precedence

Solving a "expression must have pointer to class type" error


class testClass { public: int B, C; };

testClass u;
testClass * k = &u;
testClass ** m = &k;

*m->B = 1;//Error appears here

I think I've followed the rules of pointer referencing correctly. I still have no idea why this is happening. Could someone help me?


Solution

  • operator-> has higher precedence than operator*, so *m->B is equivalent as *(m->B); while m->B is not valid here.

    You should change it to (*m)->B.