trying to use an instance of one class in another class...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
using std::cout;
using std::endl;

struct myClassA
{
  int* my_array; //getting ready to make a dynamically allocated array

  myClassA()
  {my_array=new int[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*'

Anybody know a fix?
Last edited on
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]
oh, cool, that fixed it.

thanks.
Topic archived. No new replies allowed.