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

I have implemented the pure virtual method in the derived class but the compiler complains that I didn't

本文关键字:方法 编译器 虚拟 派生 实现      更新时间:2023-10-16

我有一个抽象基类及其派生类,尽管我在派生类的 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);
};

派生.cpp:

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

主.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:UsersaubDocumentsVisual Studio 2013ProjectsProject22Project22main.cpp 10

您有一些构建问题。可能是链接了派生对象文件的过时版本?还是派生.cpp根本没有被编译并链接到项目中?

从头开始进行全新重建。