inline functions not linking?

Started by
2 comments, last by Nauht 17 years ago
Hi, I'm using Visual Studio 2003 at the moment and I have a few of these inline functions within a C++ class:

inline C3DModel* GameObject::getModel()
{
	return &mModel
}
I have set the flag /Ob1 so that the compiler would expand inline functions however when it comes to linking, the compiler complains that it can't find the inline functions:

app.obj : error LNK2019: unresolved external symbol "public: class C3DModel * __thiscall GameObject::getModel(void)" (?getModel@GameObject@@QAEPAVC3DModel@@XZ) referenced in function "void __cdecl DrawModel(class GameObject *)" (?DrawModel@@YAXPAVGameObject@@@Z)
Release/assign2.exe : fatal error LNK1120: 1 unresolved externals
The way I get around this is by defining the inline function within the class declaration but does anybody know what I did wrong when I try to use the inline keyword? Thanks.
Advertisement
An inline function is automatically given internal linkage, which means that if it is defined inside a cpp file, it can't be seen in any other file. If you want other files to use it, you must put it in a header file so that it can be defined where ever it is needed.
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
Did you originally have the function definition in the header file for the class, or in the source file (.cpp)?
I have the declaration within the class in the header file:

class GameObject{public:    C3DModel* getModel();...}


But where I define the function and use the inline keyword is in the .cpp file. So you're right John - reading this (http://msdn2.microsoft.com/en-us/library/bw1hbe6y(VS.71).aspx) MSDN article more closely, it says:
"A class member function defaults to external linkage unless a definition for that function contains the inline specifier. "

Thanks for the pointer!

This topic is closed to new replies.

Advertisement