Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
283 views
in Technique[技术] by (71.8m points)

c++ - inline function linker error

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

in the header file:

int GetTplLSize();

in the .cpp file:

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

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~1STANLEYLOCALS~1TEMPMEX_HN~1emplib.x and object C:DOCUME~1STANLEYLOCALS~1TEMPMEX_HN~1emplib.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~1MATLABR2008BBINMEX.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)?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...