开发者

linking error in Visual c++ when trying to inline functions

When trying to inline a function in Visual C++ (2008 express), I got a linking error, after a close inspection of the code, what I discovered is that the function I am trying to inline throws an exception... if I remove the exception throwing, the linking error goes away, can someone explain why it is the case?

int com_ximpleware::FastIntBuffer::intAt(int index){
    if (index < 0 || index > size - 1) {
       throw InvalidArgumentException("invalid index range in FastIntBuffer's intAt()");
    }
    return ((int *) al->get(index>>exp))[index & r];
}

The exaxt error is shown below:

intHash.obj : error LNK2019: unresolved external symbol "public: int __thiscall com_ximpleware::FastIntBuffer::intAt(int)" (?intAt@FastIntBuffer@com_ximpleware@@QAEHH@Z) referenced in funct开发者_如何学JAVAion __catch$?isUnique@IntHash@com_ximpleware@@QAE_NH@Z$0
1>C:\new_cvs\cpp_test1\Debug\cpp_test1.exe : fatal error LNK1120: 1 unresolved externals

One more thing, this function was invoked else normally, the calling part of the function wasn't included.


You'll have to place your definition in the header file.

An inline member function can be defined in two ways:

  • Define the member function in the class body itself.
  • Declare the member function just like a normal member function inside the class but when you define the inline member function, prepend the member function's definition with the keyword inline and put the definition into a header file, like the following:

class ClassA
{
public:
    void f();
};

inline void ClassA::f()
{
}

If you put the inline function's definition into a .cpp file and it if it is called from some other .cpp file, you'll get an "unresolved external" error (C++ FAQ Lite) from the linker.

I don't think it has something to do with the exception being thrown though. I could replicate this problem in Visual C++ 2005 even if the exception is not thrown.

My guess is that it was trying to inline the function but couldn't find its definition, since it was on a separate .cpp file. The compiler probably made some special processing because it recognized the inline keyword in the definition. If you remove the inline keyword, the problem disappears and linking is successful. Therefore, you need to place the definition together with its declaration in the header file, if you want it to be treated as inline.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜