Home > Back-end >  While I want to use the data members from ,h file to the other file
While I want to use the data members from ,h file to the other file

Time:08-11

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?

CodePudding user response:

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.

  •  Tags:  
  • c
  • Related