C++ 对虚拟基类析构函数的“未定义引用”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13444800/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 17:19:35  来源:igfitidea点击:

"undefined reference" to Virtual Base class destructor

c++abstract-classundefined-reference

提问by noctilux

Possible Duplicate:
What is an undefined reference/unresolved external symbol error and how do I fix it?

可能的重复:
什么是未定义的引用/未解析的外部符号错误,我该如何解决?

I have some experience with Java, and am now doing a C++ course. I wanted to try writing an interface, but I have run into some trouble with destructors which I have not been able to resolve, even with the help on the Internet... Here's my code:

我有一些 Java 经验,现在正在学习 C++ 课程。我想尝试编写一个接口,但我遇到了一些我无法解决的析构函数的问题,即使在互联网上的帮助下......这是我的代码:

    class Force {

    public:

    virtual ~Force();
    virtual VECTOR eval(VECTOR x, double t);

};

class InvSquare : public Force {

    public:

    InvSquare(double A) {

        c = A;

    }

    ~InvSquare(){};

    VECTOR eval(VECTOR x, double t) { // omitted stuff }

    private:
    double c;

};

I have tried to declare a virtual destructor for the base class, and a non-virtual one for the derived class, but I get an error saying "undefined reference to `Force::~Force()'". What does it mean, and how can I fix it?

我试图为基类声明一个虚拟析构函数,并为派生类声明一个非虚拟析构函数,但我收到一条错误消息“对`Force::~Force()'的未定义引用”。这是什么意思,我该如何解决?

Forgive me if this is a silly question!

如果这是一个愚蠢的问题,请原谅我!

Thank you very much for your help, noctilux

非常感谢你的帮助,夜光

回答by Mike Seymour

You've declared the destructor, but not defined it. Change the declaration to:

你已经声明了析构函数,但没有定义它。将声明更改为:

virtual ~Force() {}

to define it to do nothing.

定义它什么都不做。

You also want to make all the functions in the abstract interface pure virtual, otherwise they will need to be defined too:

您还希望将抽象接口中的所有函数设为纯 virtual,否则它们也需要定义:

virtual VECTOR eval(VECTOR x, double t) = 0;