Reputation: 33
Friends I defined a stack class, which makes stack of a structure, and an other class which uses stack (creating dynamically) like below
struct A{
int a;
.....
};
class stack{
private:
int head,max;
A* data; // pointer of structure 'A'
public:
stack(int length){ // constructor to allocate specified memory
data = new A[length];
head = 0;
max = length;
}
void push(A){....} //Accepts structure 'A'
A pop(){.......} //Returns structure 'A'
};
//Another class which uses stack
class uses{
private:
stack* myData;
void fun(A); //funtion is accepts structure 'A'
..........
public:
uses(int len){
myData = new stack(len); //constructor is setting length of stack
}
};
void uses::fun(A t){
A u=t;
....done changes in u
myData.push(u); //error occurs at this line
}
Now the problem is when I compile it error appears which says "Structure required on left side of . or .*"
I test stack class in main by creating objects of Structure and pushed into stack and poped which worked! it means my stack class working fine.
I know this error happen when we try to call construction without providing required arguments but I am giving values, so why this error is occurring.
Upvotes: 0
Views: 70
Reputation: 1
To fix the compiler error, you have two options as mentioned in my comment:
stack* myData;
to stack myData;
myData.push(u);
to myData->push(u);
Preferable design is the 1st option.
To make the 1st option work you should use the member initializer list of your constructor:
class uses{
private:
stack myData;
public:
uses(int len) : myData(len) {
}
};
Upvotes: 2