对类的'typeinfo'的未定义引用

时间:2012-01-21 09:15:15

标签: c++ g++ linker-errors virtual typeinfo

  

可能重复:
  g++ undefined reference to typeinfo
  Undefined symbols “vtable for …” and “typeinfo for…”?

我不能用我的班级。

class Accel {
public:
    virtual void initialize(void);
    virtual void measure(void);
    virtual void calibrate(void);
    virtual const int getFlightData(byte);
};
class Accel_ad : public Accel {
public:
    Accel_ad() : Accel(){}
    void initialize(void) {/*code code code...*/}
    void measure(void) {/*measure code*/}
    const int getFlightData(byte axis){/*getting data*/}
    void calibrate(void) {
        int findZero[FINDZERO];
        int dataAddress;
        uint8_t  data[2];
        for (byte calAxis = XAXIS; calAxis < ZAXIS; calAxis++) {
            if (calAxis == YAXIS) dataAddress = 0x32;
            if (calAxis == XAXIS) dataAddress = 0x34;
            if (calAxis == ZAXIS) dataAddress = 0x36;
            for (byte i=0; i<FINDZERO; i++) {
                readReverseWordI2C(dataAddress,data);
                findZero[i] = (data[1] << 8) | (data[0]);;
                Delay_ms(10);
            }
            accelZero[calAxis] = findMedian(findZero, FINDZERO);
        }

        // replace with estimated Z axis 0g value
        accelZero[ZAXIS] = (accelZero[XAXIS] + accelZero[PITCH]) / 2;
        // store accel value that represents 1g
        measure();
        accelOneG = -accelData[ZAXIS];
    }

的main.cpp

Accel_ad accel;//all ok
accel.initialize();//all ok
accel.calibrate();//ERROR: undefined reference to `typeinfo for Accel'
accel.measure();//All okey

谁知道什么是错的?

我得到的整个错误是:

  

。\ obj \ main.o :(。rodata._ZTI8Accel_ad [Accel_ad的类型信息] + 0x8):未定义的引用`typeinfo for Accel'[cc] collect2:ld返回1退出状态

1 个答案:

答案 0 :(得分:43)

您必须在基类中提供虚函数的定义或将它们声明为纯:

class Accel {
public:
   virtual void initialize(void) = 0;        //either pure virtual
   virtual void measure(void) = 0; 
   virtual void calibrate(void) {};          //or implementation 
   virtual const int getFlightData(byte) {};
};