我刚刚遇到了类继承代码的问题:编译器无法识别构造函数。也没有任何成员职能。 例如,如果我调用构造函数, 我的testfile(test.cpp)像这样开始:
#include "salariedemployee.h"//This is a class inherited from 'employee.h', the base class
#include "Administrator.h"//This is a class inherited from "salariedemployee.h"
#include <iostream>
#include <string>
using std::cout;
using std::endl;
using namespace employeessavitch;
int main()
{
Employee boss("Mr Big Shot","987-65-4321");//I try to call constructor in the base class "employee";
}
编译器会出现类似
的错误undefined reference to `employeessavitch::Employee::Employee(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
如果我尝试在继承的类中调用构造函数,例如
SalariedEmployee boss("Mr Big Shot","987-65-4321",10500.50);//a constructor about name, SSN number, and salary
它会出现如下错误:
undefined reference to `employeessavitch::SalariedEmployee::SalariedEmployee(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, double)'
我想知道出了什么问题?
我的基类头文件写为:
#ifndef EMPLOYEE_H
#define EMPLOYEE_H
#include <string>
using namespace std;
namespace employeessavitch
{
Employee( );//default constructor
Employee(string the_name, string the_ssn); constructor about name and ssn
}#endif
我继承的类头文件写为:
#ifndef SALARIEDEMPLOYEE_H
#define SALARIEDEMPLOYEE_H
#include <string>
#include "employee.h"
using namespace std;
namespace employeessavitch
{
class SalariedEmployee : public Employee
{
public:
SalariedEmployee( );//default constructor
SalariedEmployee (string the_name, string the_ssn, double the_weekly_salary);//constructor about name, ssn and salary
//Other member function and variables are ommitted here.
}#endif
我很确定命名空间是可以的,因为我可以写std cin和cout。
我的cpp实现文件就像:
#include <string>
#include <cstdlib>
#include <iostream>
#include "employee.h"
using namespace std;
namespace employeessavitch
{
Employee::Employee( ) : name("No name yet"), ssn("No number yet"), net_pay(0)
{
//deliberately empty
}
}
和
#include <iostream>
#include <string>
#include "salariedemployee.h"
using namespace std;
namespace employeessavitch
{
SalariedEmployee::SalariedEmployee( ) : Employee( ), salary(0)
{
//deliberately empty
}
SalariedEmployee::SalariedEmployee(string the_name, string the_number, double the_weekly_salary): Employee(the_name, the_number), salary(the_weekly_salary)
{
//deliberately empty
}
}
答案 0 :(得分:2)
我认为Employee构造函数未声明为类定义的一部分。
在employee.h中尝试:
namespace employeessavitch
{
class Employee
{
public:
Employee( );//default constructor
Employee(string the_name, string the_ssn); //constructor about name and ssn
};
}
答案 1 :(得分:0)
未定义的引用只是意味着即使它获取了函数定义(即来自头文件),也没有获得函数的实现(SalariedEmployee
和Employee
)。您是否在{。1}}的.cpp文件中实现了该功能?
答案 2 :(得分:0)
下面的代码应该可以使用(也为你的其他类做这个):
#ifndef EMPLOYEE_H
#define EMPLOYEE_H
#include <string>
using namespace std;
namespace employeessavitch
{
Employee( )
{
}//default constructor
Employee(string the_name, string the_ssn)// constructor about name and ssn
{
}
}#endif
或者,如果您认为已经在.cpp文件中定义了实现,请检查以确保参数匹配。 cpp实现应该如下所示:
employeessavitch::Employee::Employee()
{
}
employeessavitch::Employee::Employee(string the_name, string the_ssn)
{
}
此外,最好不要在头文件中使用'using namespace'语句。原因是这意味着包含此头文件的任何代码都会自动使用命名空间,这会破坏目的。如果您不想拥有cpp文件,请在头文件中使用'std ::'前缀而不是'using namespace std'。
我不确定你是否有一个包含构造函数实现的单独的.cpp文件。如果您这样做,发布它将有助于您解决问题。
答案 3 :(得分:0)
您必须将两个cstors的定义添加到employee.cpp文件中。
这个没有定义:
Employee(); //cstor
Employee( string the_name, string the_ssn); // <-- This one isn't defined
将两个cstor定义添加到您的文件中并编译:
#include <string>
#include <cstdlib>
#include <iostream>
#include "employee.h"
using namespace std;
namespace employeessavitch
{
Employee::Employee( ) : name("No name yet"), ssn("No number yet"), net_pay(0)
{
//deliberately empty
}
Employee::Employee( string the_name, string the_ssn) // <-- is missing in
{ // your cpp file
//deliberately empty
}
}