我在派生类中实现了纯虚方法,但编译器抱怨我没有

时间:2016-06-10 18:17:35

标签: c++ inheritance abstract-class virtual pure-virtual

我有一个抽象基类及其派生类,虽然我在派生类的cpp文件中实现了纯函数,当我尝试创建派生的对象时,我仍然得到一个错误,表示派生类是抽象的!! 基:

class Base  {
protected:
    string name;
    int quantity;
    double price;
public:
    c'tor....
    virtual ~Base(){}
    virtual void buy(int num) = 0;
    virtual Base& operator+(const Base& b)=0;
    };

派生:

#include "Base.h"

class Derived : public Base{
protected:
    double percentage;
public:
    c'tor...
    virtual ~Derived() {}
    virtual void buy(int num);
    virtual Derived& operator+(const Derived& d);

};

Derived.cpp:

#include "Derived.h"
void Derived::buy(int num){
   //implementation
}
Derived& Derived::operator+(const Derived& d){
   //implemetation
}

main.cpp中:

#include "Base.h"
#include "Derived.h"
int main()   {
    Derived d;
    //...
}

错误:

1   IntelliSense: object of abstract class type "Derived" is not allowed:
            pure virtual function "Base::buy" has no overrider  c:\Users\aub\Documents\Visual Studio 2013\Projects\Project22\Project22\main.cpp 10

1 个答案:

答案 0 :(得分:-4)

你有一些构建问题。可能是链接的Derived对象文件的陈旧版本?或者Derived.cpp没有被编译并链接到项目中?

从头开始重新进行重建。