donalmg
donalmg

Reputation: 655

Undefined reference from virtual function

I have a class like:

Base.h

class Base {
    public:
        Base();

        virtual int getInfo(int i);

    protected:
        int GetDetail (int iVal);

}

inline int Base::getInfo(int i){

     int output = GetDetail(i);
     return output;
};

Ib Base.cpp, I have the GetDetail defined.

int Base::GetDetail(int i){
int output;
// do work to output
return output;
}

I have some derived classes that call GetDetail from their own implementation getInfo().

When I remove the call to getInfo() from the virtual function definition in Base.h, the code will compile, with the derived classes own implementation.

When I compile with the virtual function calling GetDetail from the inlined virtual function, the linking fails with:

Undefinied reference to GetDetail.

Any ideas?

Upvotes: 0

Views: 3533

Answers (2)

NPE
NPE

Reputation: 500933

  1. Add the missing semicolon after class Base {...};
  2. Make sure you link Base.o into your executable.

For reference, I can successfully compile/link your example (having added the semicolon) with g++ 4.4.3.

Upvotes: 5

Kiril Kirov
Kiril Kirov

Reputation: 38193

Remove the inline specifier in front of int Base::getInfo(int i) and try (:

Upvotes: 1

Related Questions