#include <iostream>
using std::cout;
using std::endl;
struct myClassA
{
int* my_array; //getting ready to make a dynamically allocated array
myClassA()
{my_array=newint[5];} //dynamically allocates array, my_array, with arbitrary size 5
};//end of myClassA structure
struct myClassB
{
myClassA* pt; //I want to be able to use an instance of myClassA as a member in myClassB. I intend to pass it via the constructor.
myClassB(myClassA* myPt) //Constructor takes pointer to instance of myClassA
{pt=myPt;} //I set myClassB's member, pt, to the pointer passed as an argument in the constructor
void myMethod()
{cout<<*pt.my_array[2]<<endl;} //theoretically this should work right??
//error: request for member 'my_array' in '((myClassB*)this)->myClassB::pt', which is of non-class type 'myClassA*'
};
int main()
{return 0;}
The error is at compile-time:
error: request for member 'my_array' in '((myClassB*)this)->myClassB::pt', which is of non-class type 'myClassA*'
Wrong. The . operator has a higher precedence order than the dereference operator. This is what you meant to do: (*pt).my_array[2]. And this is how you should do it: pt->my_array[2]