编译精细,建筑有错误&未定义参考'

时间:2014-04-10 16:15:57

标签: c++ class inheritance virtual-method

我正在搞乱类和虚方法并且遇到了问题。 就像标题所说,我可以编译好,当我尝试构建然后执行程序时问题就出现了。 我收到了这些错误:

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;
}

我正在尝试学习虚拟方法和类的工作原理,我知道它不是最好的代码,但我只是想知道它为什么不打印出来。我以为我已经宣布了所有的一切。我最终想尝试添加一辆来自车辆的卡车类,然后在这个程序中添加一些类型的卡车,但是当我无法获得输出时我决定停下来。

3 个答案:

答案 0 :(得分:2)

您已声明,但未在Vehicle中实现虚拟功能。

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

class vehicle{
public:

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

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

答案 1 :(得分:0)

您必须在课程vehicle中实施方法:

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 vehicle{
public:

    virtual void openDoor() = 0;
    virtual void turnOnFrontLights() = 0;
    virtual void turnOnBackLights() = 0;
    virtual void shiftGear() = 0;
    virtual void openHood() = 0;
    virtual void openTrunk() = 0;
    virtual void checkEngine() = 0;
    virtual void moveSeat() = 0;
    virtual void useSeatBelt() = 0;
    virtual void useBrake() = 0;
}; 

答案 2 :(得分:0)

  1. 由于类Vehicle中的方法没有实现,请将它们设为pure virtual,从而创建类abstract

  2. 在课程Car中,也要制作方法virtual,因为您要在派生类MiniCooper中覆盖它们。