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

Handling multiple .h and .cpp files

The first .h file is named as "Employee.h"

#include<iostream>

class Employee
{
  public:
    int total=0;
    std::string Name,Id,Address;
    void dummy();
};

The first .cpp file is named as EMployee.cpp

#include "Employee.h"

void Employee::dummy()
{
        std::cout<<"Rohith"<<std::endl;
}

And the main file is named as main.cpp

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"
int main()
{
 std::cout<<total<<std::endl;
 Employee obj;
 obj.dummy();
}

When executing the above cpp files i am getting an error like this

g++ Employee.cpp main.cpp
main.cpp: In function ‘int main()’:
main.cpp:5:13: error: ‘total’ was not declared in this scope
std::cout<<total<<std::endl;
^~~~~
main.cpp:5:13: note: suggested alternative: ‘strtol’
std::cout<<total<<std::endl;
^~~~~
strtol

Can you help me in this? how can i resolve the above problem.

>Solution :

According to Employee.h, you intend dummy to be a non-static method of class Employee.

In this case you should do the following changes:

  1. In Employee.cpp, change the definition of dummy to:
void Employee::dummy()
{
    std::cout<<"Rohith"<<std::endl;
}

Without qualifying the name dummy with Employee::, you are defining a new free function called dummy, rather than the method of Employee.

  1. When invoking the dummy method you need an object of class Employee, e.g.:
Employee emp;
emp.dummy();
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