0

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.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • 1
    Change `stack* myData;` to `stack myData;` or `myData.push(u);` to `myData->push(u); `to fix the compiler error. Preferably the 1st option. – πάντα ῥεῖ May 14 '15 at 20:15
  • @Muhammad Zubair ALi In the future, when you post such questions, you might want to add the line # about which the compiler complains, or possibly even copy the compiler's output verbatim. – Ami Tavory May 14 '15 at 20:18
  • If I make it ` stack myData; ` then how i will set the size of stack which is being setup in constructor.. Thank you very much for response.. – Muhammad Zubair ALi May 14 '15 at 20:23
  • myData->push(u) solved the problem !! Thanks again – Muhammad Zubair ALi May 14 '15 at 20:27
  • @MuhammadZubairALi _"then how i will set the size of stack which is being setup in constructor.."_ You use a member initializer list: `uses(len) : myData(len) {}`. See also here for instance: http://stackoverflow.com/questions/1711990/what-is-this-weird-colon-member-syntax-in-the-constructor – πάντα ῥεῖ May 14 '15 at 20:31
  • Actually I am new to programming so my question may sound stupid but I would like to know how may I set the size of stack at runtime when I am declaring it without pointer. – Muhammad Zubair ALi May 14 '15 at 20:32
  • @MuhammadZubairALi See my answer. Also you should fix those missing parameter types in your function declarations. – πάντα ῥεῖ May 14 '15 at 20:38

1 Answers1

2

To fix the compiler error, you have two options as mentioned in my comment:

  1. Change stack* myData; to stack myData;
  2. Change 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) {
    }
};
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190