开发者

Undefined reference from virtual function

开发者 https://www.devze.com 2023-02-17 22:14 出处:网络
I have a class like: Base.h class Base { public: Base(); virtual int getInfo(int i); protected: int GetDetail (int iVal);

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 definiti开发者_JS百科on 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?


  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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消