内联函数链接器错误 [英] inline function linker error

查看:287
本文介绍了内联函数链接器错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用特定类的内联成员函数。例如,没有内联的函数声明和实现如下:

I am trying to use inline member functions of a particular class. For example the function declaration and implementation without inlining is as such:

在头文件中:

int GetTplLSize();

int NeedleUSsim::GetTplLSize()
{
    return sampleDim[1];
}

由于某种原因,如果我将inline实现和声明,以及在这两个地方,我得到如下所示的链接器错误:

For some reason if I put the "inline" keyword in either one of the implementation and declaration, as well as in both places, I get linker errors as shown:


 Creating library C:\DOCUME~1\STANLEY\LOCALS~1\TEMP\MEX_HN~1\templib.x and object C:\DOCUME~1\STANLEY\LOCALS~1\TEMP\MEX_HN~1\templib.exp 
mexfunction.obj : error LNK2019: unresolved external symbol "public: int __thiscall NeedleUSsim::GetTplLSize(void)" (?GetTplLSize@NeedleUSsim@@QAEHXZ) referenced in function _mexFunction 
mexfunction.mexw32 : fatal error LNK1120: 1 unresolved externals 

  C:\PROGRA~1\MATLAB\R2008B\BIN\MEX.PL: Error: Link of 'mexfunction.mexw32' failed. 

为了摆脱这个错误,需要做些什么(例如,在做这些内联成员函数时我做错了什么) ?

What needs to be in order to get rid of this error (i.e. what am I doing wrong in terms of making these inline member functions)?

推荐答案

您需要将函数定义放入标题。提示编译器内联的最简单的方法是在类声明中包含方法体:

You need to put function definition into the header then. The simplest way to hint the compiler to inline is to include method body in the class declaration like:


class NeedleUSsim
{
  // ...
  int GetTplLSize() const { return sampleDim[1]; }
  // ...
};

或者,如果您坚持单独声明和定义:

or, if you insist on separate declaration and definition:


class NeedleUSsim
{
  // ...
  int GetTplLSize() const;
  // ...
};

inline int NeedleUSsim::GetTplLSize() const
{ return sampleDim[1]; }

定义必须在使用该方法的每个翻译单元中可见。

The definition has to be visible in each translation unit that uses that method.

这篇关于内联函数链接器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆