“undefined reference” to Virtual Base class destructor [duplicate]

随声附和 提交于 2019-12-04 15:16:36

问题


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:

    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?

Forgive me if this is a silly question!

Thank you very much for your help, noctilux


回答1:


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 VECTOR eval(VECTOR x, double t) = 0;


来源:https://stackoverflow.com/questions/13444800/undefined-reference-to-virtual-base-class-destructor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!