Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

While I want to use the data members from ,h file to the other file

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

 #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?

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading