jsad923f

I made a class in the header file

class Foo
{
public:
float area(float x, floaty);
};

in the .cpp I tried

#include "Foo.h"

inline float Foo::area(float x, floaty)
{
//lot's and lot's and lot's of stuff
.
.
return variable;
}

and I get...


error LNK2019: unresolved external symbol "public: float __thiscall Foo::area(float x, floaty)"

If I take away the inline everything works. No, we don't want the code to be in the header.

What am I doing wrong ( 2003 .NET)


Re: Visual C++ Language LNK 2019, inline functions

einaros

If you don't need an inlined function, don't add the "inline" keyword when you define it in your source file. If, however, you want the function to be considered for inlining, either:

  1. add "inline" to the declaration within the class definition, and implement the function in the header.
  2. implement the function in the class definition.





Re: Visual C++ Language LNK 2019, inline functions

jsad923f

Due to our coding standards no function may be implemented in the header.

I've tried adding "inline" to the funciton declaration in the header, to the implementation in the .cpp file, and to both. Hell, I even reduced the code found in the .cpp to {return x*y;} to see if it was something I was doing inside the code that was messing it up.

This is, of course, a class I made up to see what was going on in the real problem. The main class consists of

int _main(int argc, _TCHAR* argv)
{
Foo here;
here.area(4,5);
}

with Foo.h and Foo.cpp the only files found within the project.

Still gave me the error. I don't believe it is something to do with the code I am writing itself. Actually I'm convinced it isn't for it works perfectly fine inside VC++ 6.0. We're just not allowed to use 6.0, go figure.




Re: Visual C++ Language LNK 2019, inline functions

einaros

The simple answer is that you cannot inline a function and define it in a .cpp unless it's only used from that same .cpp-file. Your only options, if you are to use this from other .cpp-files is to either don't declare it inline in the first place, or if you do so: define it in your header.

There's really no other way around this. If it fits your coding standard you could have other .cpp files include the .cpp you define the inline functions in, but that would just make a mockery of it all -- it'd be a header file with a different extension.






Re: Visual C++ Language LNK 2019, inline functions

jsad923f

Damn. Well, back to the drawing boards.

Thanks for the advice. I do appreciate it.




Re: Visual C++ Language LNK 2019, inline functions

Mike Danes

Maybe it's worth adding that VS 2003 & 2005 support what is called "link time code generation". This allows the compiler to inline functions even if you are not using inline and the function is defined in a .cpp file. Of course the compiler itself will decide what functions get inlined but this is not much different from using the inline keyword because even in that case the compiler may choose to not inline a function.

http://msdn2.microsoft.com/en-us/library/xbf3tbeh(VS.71).aspx





Re: Visual C++ Language LNK 2019, inline functions

einaros

Mike Danes wrote:

Maybe it's worth adding that VS 2003 & 2005 support what is called "link time code generation". This allows the compiler to inline functions even if you are not using inline and the function is defined in a .cpp file. Of course the compiler itself will decide what functions get inlined but this is not much different from using the inline keyword because even in that case the compiler may choose to not inline a function.

Do you know if there's any kind of weighing involved, with regard to using LTCG contra implicit inlining through class definition I mean, will the inlining typically be more aggressive in either of the cases I've never seen any clear statement on that.






Re: Visual C++ Language LNK 2019, inline functions

Mike Danes

" I've never seen any clear statement on that."

Neither do I.