c++ - 编译正常,构建有错误 'undefined reference to'

标签 c++ class inheritance virtual-functions

我在处理类和虚拟方法时遇到了问题。 正如标题所说,我可以很好地编译,但当我尝试构建然后执行程序时,问题就来了。 我收到这些错误:

In function 'vehicle::vehicle()';
undefined reference to 'vtable for vehicle'
undefined reference to 'typeinfo for vehicle'

我已经研究了这些问题,但还没有找到适合我给定困境的解决方案。当我无法到达任何地方时,我决定来这里。

这是我的代码:

using namespace std;
#include <iostream>
#include <stdio.h>
#include <string.h>

class vehicle{
public:

    virtual void openDoor();
    virtual void turnOnFrontLights();
    virtual void turnOnBackLights();
    virtual void shiftGear();
    virtual void openHood(); 
    virtual void openTrunk();
    virtual void checkEngine();
    virtual void moveSeat();
    virtual void useSeatBelt();
    virtual void useBrake();
}; 
class Car : vehicle{
public:
     void openDoor() {std::cout << "I might be able to."<< endl;}
     void turnOnFrontLights() {std::cout << "I might be able to."<< endl;}
     void turnOnBackLights() {std::cout<< "I might be able to."<< endl;}
     void shiftGear() {std::cout <<"I might be able to." << endl;}
     void openHood() {std::cout <<"I might be able to."<< endl;}
     void openTrunk(){std::cout<<"I might be able to." << endl;}
     void checkEngine(){std::cout <<"I might be able to." << endl;}
     void moveSeat(){std::cout << "I might be able to."<< endl;}
     void useSeatBelt(){std::cout << "I might be able to."<< endl;}
     void useBrake(){std::cout << "I might be able to."<< endl;}
};
class MiniCooper : Car{
public:
     void openDoor() {std::cout << "I can."<< endl;}
     void turnOnFrontLights() {std::cout << "I can."<< endl;}
     void turnOnBackLights() {std::cout<< "I can."<< endl;}
     void shiftGear() {std::cout <<"I can." << endl;}
     void openHood() {std::cout <<"I can."<< endl;}
     void openTrunk(){std::cout<<"I can." << endl;}
     void checkEngine(){std::cout <<"I can." << endl;}
     void moveSeat(){std::cout << "I can."<< endl;}
     void useSeatBelt(){std::cout << "I can."<< endl;}
     void useBrake(){std::cout << "I can."<< endl;}
};


int main(){
MiniCooper *miniCooper = new MiniCooper;
cout << "Can you open the doors?" << endl;
miniCooper->openDoor();
return 0;
}

我正在尝试了解虚拟方法和类的工作原理,我知道这不是最好的代码,但我只是想知道为什么它没有打印出来。我以为我已经正确申报了一切。我最终想尝试添加一个来自 vehicle 的卡车类,然后也向该程序添加一些类型的卡车,但是当我无法获得输出时决定停止。

最佳答案

您已在 Vehicle 中声明但未实现您的虚函数。

如果您不想实现它们,那是可能的。您必须将虚函数声明为纯虚函数:

class vehicle{
public:

    virtual void openDoor()=0;
    //                     ^^
    // Notice the trailing "equal-zero"
    ...
};

这使得 Vehicle 成为无法实例化的抽象类,并强制客户端代码使用 Vehicle 的派生类。

关于c++ - 编译正常,构建有错误 'undefined reference to',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22993503/

相关文章:

c++ - 从派生类访问基类成员时强制使用范围解析运算符

c++ - 创建 X 数量的无名对象

c++ - OOP 设计 - 从具有通用类型的容器继承

c++ 和 gtest,需要帮助理解为什么看似简单的测试失败

java - 如何将我的 ClassLoader 设置为 JVM 的 ClassLoader 来加载所有类(包括 jar 类)?

C++ 访问冲突类数组

python - 从元组子类继承

javascript - 如何防止 DOM 子级继承 addEventListener?

java - super(Application.class); 是什么意思?在Java中?

javascript - 难以将 switch 语句与类和 for 循环一起使用