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

jsh6303

我只是遇到了类继承代码的麻烦:编译器无法识别构造函数。也不是任何成员函数。例如,如果我调用构造函数,我的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
    }
 }
卡梅斯瓦里布

我相信Employee构造函数未声明为类定义的一部分。

在employee.h中尝试以下操作:

namespace employeessavitch
{
  class Employee
  {
    public:
    Employee( );//default constructor
    Employee(string the_name, string the_ssn); //constructor about name and ssn
  };
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

可变参数类模板和继承-默认编译器生成的构造函数

与子构造函数同名的继承的类成员

编译器无法推断泛型类构造函数类型的原因是什么?

MSVC 无法识别继承模板类的模板类的“直接”基类构造函数

当类具有引用成员时,为什么C ++编译器不删除副本构造函数?

Closure编译器导出Typescript类和函数

无法识别的继承函数

继承构造函数和虚拟基类

继承的类和父级的构造函数

C ++ 11编译器的构造函数继承产生错误

类构造函数和成员变量(字段)

使用参数化构造函数初始化类成员-编译器认为我正在声明一个函数?

将类模板转换为 SFINAE 和构造函数委托,现在我收到编译器错误

关于继承和构造函数中 Java 8 编译器增强的澄清

在继承的类构造函数中使用隐藏成员

打字稿:无法访问继承的类构造函数中的成员值

我可以要求编译器禁止定义模板类成员函数的通用版本吗?

编译器似乎找不到llvm :: Instruction类的成员函数

模板类的std线程调用模板成员函数:编译器错误

编译器在调用基类构造函数之前会在子类构造函数中寻找** super **关键字吗?

使用类的命名空间参数化构造函数时的编译器错误

编译器未针对类的构造函数抛出语法错误

继承基类的构造函数

Android Studio编译器无法识别GraphicOverlay类

编译器无法识别模板化类

TypeScript编译器无法识别导出的类

编译器是否需要内联基类中的虚函数和最终函数?

编译器错误和String类无法识别方法

AngularJS控制器构造函数和编译器