0

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?

donalmg
  • 627
  • 5
  • 15
  • 22
  • 1
    Are you sure the missing `;` in the `int output = GetDetail(i)` is not the problem? – Asha Mar 17 '11 at 11:28
  • 2
    Is "return int" a typo? If you have inlined a virtual function, then it's important to be aware what that means. http://stackoverflow.com/questions/2130226/inline-virtual-function explains in more detail. – Jeff Foster Mar 17 '11 at 11:29
  • Edited to make clearer. Both typos. – donalmg Mar 17 '11 at 11:31
  • Why did you make the function inline? Have you already run a profiler on your application? – Alessandro Teruzzi Mar 17 '11 at 11:31
  • 1
    Virtual functions will not get inlined anyway, so move the implementation of getInfo in the cpp file first. Then make sure you are linking correctly. There should not be any error. – Alexandre C. Mar 17 '11 at 11:34
  • 1
    Did you get this the right way round? Surely GetDetail should be the virtual function called by the public non-virtual getInfo() ? btw, the end of Base class definition also needs a semi-colon – CashCow Mar 17 '11 at 11:41

2 Answers2

4
  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.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
2

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

Kiril Kirov
  • 37,467
  • 22
  • 115
  • 187