类继承:编译器无法识别类的构造函数和成员函数

Class inheritance: Constructor and member functions of class not recognized by compiler

本文关键字:构造函数 成员 函数 继承 编译器 识别      更新时间:2023-10-16

我的类继承代码遇到了问题:编译器无法识别构造函数。也没有任何成员职能。例如,如果我调用构造函数,我的测试文件(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
       }
    }

未定义的引用只是指即使它正在获取函数定义(即从头文件),它也没有获取函数(SalariedEmployeeEmployee)的实现。您是否在Employee(string the_name, string the_ssn);的.cpp文件中实现了该函数?