【问题标题】:'Undefined reference to ' error in GCC EclipseGCC Eclipse 中的“未定义引用”错误
【发布时间】:2013-12-15 13:24:53
【问题描述】:

我使用 Eclipse+GCC+MinGW。

AbstractValue.h:

#ifndef ABSTRACTVALUE_H_
#define ABSTRACTVALUE_H_

#include <string>


class AbstractValue {
public:
    virtual AbstractValue* add(AbstractValue*) = 0;
    virtual AbstractValue* sub (AbstractValue*) = 0;
    virtual AbstractValue* mul (AbstractValue*) = 0;
    virtual AbstractValue* div (AbstractValue*) = 0;
    virtual std::string toString () = 0;
    virtual ~AbstractValue() = 0;
};


#endif /* ABSTRACTVALUE_H_ */

IntegerValue.h

#ifndef INTEGERVALUE_H_
#define INTEGERVALUE_H_

#include "../AbstractValue/AbstractValue.h"
#include <string>

class IntegerValue: public AbstractValue {
private:
    int value;
public:
    IntegerValue(int);
    ~IntegerValue();
    int getValue();
    IntegerValue* add (AbstractValue*);
    IntegerValue* sub (AbstractValue*);
    IntegerValue* mul (AbstractValue*);
    IntegerValue* div (AbstractValue*);
    std::string toString();
};

IntegerValue.cpp:

#include "IntegerValue.h"
#include <stdlib.h>

IntegerValue::IntegerValue(int a) : value(a) {};

IntegerValue::~IntegerValue() {}


int IntegerValue::getValue() {
    return this -> value;
}

IntegerValue* IntegerValue::add (AbstractValue* another) {
    IntegerValue* anotherInteger = (IntegerValue*) another;
    int newValue = this -> getValue() + anotherInteger -> getValue();
    return new IntegerValue(newValue);
}

IntegerValue* IntegerValue::sub (AbstractValue* another) {
    IntegerValue* anotherInteger = (IntegerValue*) another;
    int newValue = this -> getValue() - anotherInteger -> getValue();
    return new IntegerValue(newValue);
}

IntegerValue* IntegerValue::mul (AbstractValue* another) {
    IntegerValue* anotherInteger = (IntegerValue*) another;
    int newValue = this -> getValue() * anotherInteger -> getValue();
    return new IntegerValue(newValue);
}

IntegerValue* IntegerValue::div (AbstractValue* another) {
    IntegerValue* anotherInteger = (IntegerValue*) another;
    int newValue = this -> getValue() / anotherInteger -> getValue();
    return new IntegerValue(newValue);
}

std::string IntegerValue::toString() {
    char *res = new char[1000];
    itoa(this -> value, res, 10);
    std::string a (res);
    delete[] res;
    return a;
}

我在IntegerValue::~IntegerValue() {} 上收到undefined reference to AbstractValue::~AbstractValue()。为什么?

【问题讨论】:

    标签: c++ gcc mingw


    【解决方案1】:

    因为

    virtual ~AbstractValue() = 0;
    

    表示纯虚拟,没有实现。这是错误的,析构函数不能是没有实现的纯虚函数。使用这个:

    virtual ~AbstractValue() {}
    

    即一个空的实现。

    (旁注:析构函数可以是纯虚拟的 WITH 实现,但这里没有理由这样做)

    【讨论】:

    • 你可以很容易地用谷歌搜索这个。此外,纯析构函数强制扩展类编写自己的并允许通过指向基类的指针删除派生对象,所以它一点用处都没有。
    • @LuchianGrigore 我从来没有见过,至少没有在派生类提供实现的地方,但我可以想象它如何有用,自定义对 Base 私有资源的处理,取决于派生类的实现。
    【解决方案2】:

    virtual 析构函数必须有一个实现(与纯virtual 方法相反,可以不实现)。

    【讨论】:

      【解决方案3】:

      任何派生类的析构函数 (~IntegerValue()) 都必须调用基类的析构函数 (~AbstractValue()),因此仍必须定义析构函数(即使它为空):

      所以,

      // In file AbstractValue.cpp
      
      AbstractValue::~AbstractValue(){ }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-10-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多