类未定义的C++

class undefined C++

本文关键字:C++ 未定义      更新时间:2023-10-16

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

test.cpp:(.text+0xb7):未定义对"partTimeEmployee(std::basic_string,std::allocater>)"的引用

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

工作.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;
}

员工.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");
}

更改

class partTimeEmployee : public partTimeEmployee

class partTimeEmployee : public Employee

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