undefined reference to `typeinfo for class'

SevenDays picture SevenDays · Jan 21, 2012 · Viewed 35.6k times · Source

Possible Duplicate:
g++ undefined reference to typeinfo
Undefined symbols “vtable for …” and “typeinfo for…”?

I can't use my class.

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

Who know what's wrong?

The entire error I get is:

.\obj\main.o:(.rodata._ZTI8Accel_ad[typeinfo for Accel_ad]+0x8): undefined reference to `typeinfo for Accel' [cc] collect2: ld returned 1 exit status

Answer

Luchian Grigore picture Luchian Grigore · Jan 21, 2012

You must either provide a definition for virtual functions in your base class or declare them pure:

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) {};
};