类继承:编译器无法识别类的构造函数和成员函数
Class inheritance: Constructor and member functions of class not recognized by compiler
我的类继承代码遇到了问题:编译器无法识别构造函数。也没有任何成员职能。例如,如果我调用构造函数,我的测试文件(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
}
}
我相信Employee构造函数并没有作为类定义的一部分进行声明。
在员工身上试试这个。h:
namespace employeessavitch
{
class Employee
{
public:
Employee( );//default constructor
Employee(string the_name, string the_ssn); //constructor about name and ssn
};
}
下面的代码应该可以工作(也可以为其他类执行此操作):
#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::"前缀,而不是"使用命名空间std"。
我不太确定您是否有一个单独的.cpp文件,其中包含构造函数实现。如果你这样做了,发布它将有助于解决你的问题。
您必须将两个cstor的定义添加到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
}
}
未定义的引用只是指即使它正在获取函数定义(即从头文件),它也没有获取函数(SalariedEmployee
和Employee
)的实现。您是否在Employee(string the_name, string the_ssn);
的.cpp文件中实现了该函数?
相关文章:
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 继承:构造函数,初始化C++11中基类的类C数组成员
- C++-我可以创建另一个类的成员并在构造函数中使用它吗
- 如果基类包含双指针成员,则派生类的构造函数
- 为什么类中的ostringstream类型的成员会导致";调用隐含删除复制构造函数";错误
- constexpr构造函数需要常量成员函数时出现问题
- 如何在c++中定义以struct为数据成员的类中的构造函数
- 在作为静态成员包含在另一个类中的类的构造函数中使用 cout
- std::ofstream 作为类成员删除复制构造函数?
- c++构造函数成员初始化:传递参数
- C++初始化类实例时隐式调用类成员的构造函数
- 基类中的默认析构函数禁用子类中的移动构造函数(如果有成员)
- 在构造函数中输入对象时C++类成员作用域
- 解释了构造函数成员初始化列表
- 构造函数成员初始值设定项跨成员列出,安全吗?
- C 构造函数成员分配优化
- 类内/构造函数成员初始化
- 在构造函数成员初始化之前调用成员函数的语法
- 非构造函数成员函数中的显式字符串行为