class undefined C ++

时间:2015-10-28 19:53:59

标签: c++ oop

在测试中我试图创建对象partTimeEmployee,但我得到一个未定义的引用错误,我不明白为什么?我不明白错误,因为我传递了正确的数据。我的.cpp和.h不包括在内吗?

test.cpp:(。text + 0xb7):对'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");
}

2 个答案:

答案 0 :(得分:2)

更改

class partTimeEmployee : public partTimeEmployee

class partTimeEmployee : public Employee

答案 1 :(得分:1)

构建应用程序时,首先将源文件(通常为.cpp)编译为目标文件(通常为.o或.obj),然后将它们链接到可执行文件中。它可以通过命令行显式完成,使用makefile或此类或IDE。您的错误显示来自partTimeEmployee.cpp编译的目标文件未包含在链接中。您没有提供有关如何构建可执行文件的足够信息,因此很难说如何修复它。