-1

I am using the two .h files and the two .cpp file.

The employee.h file contains

class Employee
{
        public:
          std::string Name,Id,Address;
};

The second .h file stack.h contains

 #include "employee.h"
class Stack
{
  public:
   int total=0;
    void push();
    void pop();
    void display();
};

The first.cpp file stack.cpp contains

#include "stack.h"

Employee obj1;
Stack emp[10];
void Stack::push()
{
  if(total>=10)
  {
    total--;
    std::cout <<"Stack is Overflowed";
  }
  else
  {
   std::cout<<"Enter data of employee "<<std::endl;
    std::cout<<"Enter employee name: ";
   std::cin>>emp[total].obj1.Name;
    std::cout<<"Enter id: ";
    std::cin>>emp[total].obj1.Id;
    std::cout<<"Enter address: ";
    std::cin>>emp[total].obj1.Address;
  }
  total++;
}

The second cpp file main.cpp contains

#include "stack.h"
Stack obj;
int main()
{
  obj.push();
}

While i am executing above files it is giving an error like this

g++ stack.cpp main.cpp
stack.cpp: In member function ‘void Stack::push()’:
stack.cpp:16:25: error: ‘class Stack’ has no member named ‘obj1’
    std::cin>>emp[total].obj1.Name;
                         ^~~~
stack.cpp:18:26: error: ‘class Stack’ has no member named ‘obj1’
     std::cin>>emp[total].obj1.Id;
                          ^~~~
stack.cpp:20:26: error: ‘class Stack’ has no member named ‘obj1’
     std::cin>>emp[total].obj1.Address;

If i remove the obj1 from stack.cpp then it will giving an error like this code:

std::cout<<"Enter data of employee "<<std::endl;
    std::cout<<"Enter employee name: ";
   std::cin>>emp[total].Name;
    std::cout<<"Enter id: ";
    std::cin>>emp[total].Id;
    std::cout<<"Enter address: ";
    std::cin>>emp[total].Address;

Error:

g++ stack.cpp main.cpp
stack.cpp: In member function ‘void Stack::push()’:
stack.cpp:16:25: error: ‘class Stack’ has no member named ‘Name’
    std::cin>>emp[total].Name;
                         ^~~~
stack.cpp:18:26: error: ‘class Stack’ has no member named ‘Id’
     std::cin>>emp[total].Id;
                          ^~
stack.cpp:20:26: error: ‘class Stack’ has no member named ‘Address’
     std::cin>>emp[total].Address;

Can anyone please help to this problem?

Aamir
  • 1,974
  • 1
  • 14
  • 18
  • 1
    `emp` is an array of `Stack` and the error clearly says that `Stack` does not have member named `Name` and `obj1`. What isn't clear from that? The program doesn't even make sense. Refer to a [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – Jason Aug 10 '22 at 17:24
  • 1
    `Stack emp[10];` gives you 10 stacks, not 10 employees. – BoP Aug 10 '22 at 17:24
  • 1
    `emp[total]` refers to a `Stack` object not an `Employee` object. – Jim Rhodes Aug 10 '22 at 17:25
  • If i change my code like Employee emp[10] instead of Stack emp[10] in stack.cpp. is it resolve my error. – potti rohith Aug 10 '22 at 17:30
  • @pottirohith Put `Employee emp[10];` **inside** `class Stack { ... };` That is the way to resolve the error. – john Aug 10 '22 at 17:32
  • But if you change your code like that, then `stack` is no longer needed. Not only that, ctually, using an array here at all defeats the whole purpose of having a stack. – Spencer Aug 10 '22 at 17:36
  • @Spencer Well it's true you would not code a stack like this for real, but this is a learning exercise. – john Aug 10 '22 at 17:38
  • @john Yes, that's OK inside the implementation of `stack`. But the change OP made appears to have been at global scope. – Spencer Aug 10 '22 at 17:40
  • @Spencer OK, fair enough, I misunderstood you. – john Aug 10 '22 at 17:49

1 Answers1

2

It seems to me that you are trying to create stack of employees. The way to do this is to put the employees inside the Stack. Like this

class Employee
{
public:
    std::string Name,Id,Address;
};

class Stack
{
public:
    int total=0;
    Employee emp[10]; // 10 employees
    void push();
    void pop();
    void display();
};

void Stack::push()
{
    std::cout<<"Enter data of employee "<<std::endl;
    std::cout<<"Enter employee name: ";
    std::cin>>emp[total].Name;
    std::cout<<"Enter id: ";
    std::cin>>emp[total].Id;
    std::cout<<"Enter address: ";
    std::cin>>emp[total].Address;
}

You already did this right with total, it's just the same for everything else that you want to be part of a Stack.

john
  • 85,011
  • 4
  • 57
  • 81
  • I need to create a stack of employees only but i have to made it like one .h file for employee class and one .h file for stack class and .cpp file for declaring methods in stack.h and finally i have to execute it in the one more main.cpp file. like this i have to do so that's why i have posted the question. – potti rohith Aug 10 '22 at 17:28
  • @pottirohith OK, but you still have to do it like I said above. Why does the code above not work for you? – john Aug 10 '22 at 17:29
  • 1
    @pottirohith Just put `#include "Employee.h"` at the top of `Stack.h`. But then declare `Stack` just the way John described. – Spencer Aug 10 '22 at 17:34
  • 1
    Additional recommendation: `push` does too much. All a function named `push` should do is push an object onto the stack. All of the user input should be elsewhere, perhaps an `operator >>` overload for `Employee`. In general a function should do one thing (and do it well) that way you have a bunch of small, easy to test, and reusable building blocks that you can build more complicated programs around. – user4581301 Aug 10 '22 at 17:56
  • @user4581301 You're right; prompting for each employee's details is a job for some UX function (in this case `main`). – Spencer Aug 10 '22 at 17:58