john wayne
john wayne

Reputation: 17

class undefined C++

In test i am trying to create object partTimeEmployee, but i get an undefined reference error, and i don't understand why? I dont understand the error because i pass it the right kind of data. is my .cpp and .h not included right?

test.cpp: (.text+0xb7): undefined reference to 'partTimeEmployee(std::basic_string, std::allocator >)'

work.h

#ifndef WORK_H
#define WORK_H

using namespace std;
#include <string>

class work{
        public:
          void DisplayMe();
          work(string x,string y);
          work();
          ~work();

        string name;
        string id;
};
#endif

work.cpp

#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib.h>
#include "work.h"

using namespace std;

work::work(){
        cout<<name<<"in default work constructor"<<endl;
}

work::~work(){
        cout<< name << " calling work destructor. " << endl;
}

work::work(string x, string y){

        name = x;
        id = y;

        cout << "in parent constructor" <<endl;
}

void work::DisplayMe(){
        cout << "NAME: " << name << "    ID#: " << id <<endl;
}

Employee.h

#ifndef EMPLOYEE_H
#define EMPLOYEE_H

include "work.h"
using namespace std;

class Employee : public sfasu{

        public:
          Employee();
          Employee(string x);
          ~Employee();

        string department;

};
#endif

Employee.cpp

#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib.h>
#include "Employee.h"

using namespace std;

Employee::Employee(){
        cout<<name<<"in default sfasu constructor"<<endl;
}

Employee::~Employee(){
        cout<< name << " calling parent destructor. " << endl;
}

Employee::Employee(string x){

        department = x;

        cout << "in parent constructor" <<endl;
}

partTimeEmployee.h

#ifndef PARTTIMEEMPLOYEE_H
#define PARTTIMEEMPLOYEE_H

#include "Employee.h"
using namespace std;

class partTimeEmployee : public Employee{

        public:
          partTimeEmployee();
          partTimeEmployee(string x);
          ~partTimeEmployee();

        string hourly_wage;

};
#endif

partTimeEmployee.cpp

#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib.h>
#include "partTimeEmployee.h"

using namespace std;

partTimeEmployee::partTimeEmployee(){
        cout<<"in default partTimeEmployee constructor"<<endl;
}

partTimeEmployee::~partTimeEmployee(){
        cout<< " calling FTP destructor. " << endl;
}

partTimeEmployee::partTimeEmployee(string x){

        hourly_wage = x;

        cout << "partTimeEmployeeconstructor" <<endl;
}

test.cpp

#include <iostream>
#include <iomanip>
#include "sfasu.h"
#include "Employee.h"
#include "partTimeEmployee.h"
using namespace std;

int main(){

partTimeEmployee one("data");
}

Upvotes: 0

Views: 94

Answers (2)

R&#255;ck N&#246;thing
R&#255;ck N&#246;thing

Reputation: 177

Change

class partTimeEmployee : public partTimeEmployee

to

class partTimeEmployee : public Employee

Upvotes: 2

Slava
Slava

Reputation: 44268

When you build application you first compile source files (usually .cpp) to object files (usually .o or .obj) and then link them together into executable. It can be done explicitly through command line, using makefile or such or IDE. Your error shows that object file from compilation of partTimeEmployee.cpp is not included in linking. You do not provide enough information how you build your executable so it is difficult to say how to fix it.

Upvotes: 1

Related Questions