Wednesday, April 25, 2007

Error LNK2001: unresolved external symbol...

Recently, when I used the templates in C++, I got a link error: unresolved external symbol "public: __thiscall ...". My work has three files: fc.h, fc.cpp and main.cpp.







------------------------------------------------

// fc.h

template <class T>

class fc

{

    public:

       func();

};

// end of fc.h//

------------------------------------------------

// fc.cpp

template <class T>fc <class T>::func() { }

// end of fc.cpp//

------------------------------------------------

// main.cpp

#include "fc.h"

int main()

{

    fc A;

    A.func();

}

------------------------------------------------







However, when I removed the template declaration from the header file, or put all three files just in one body, the link runs smoothly. Finally, I found the reason and the solution of this kind of problem from the Internet [1].

At least three cases are related to this link error:
1. Forget to define a declared member function, especially the constructor.
2. Static member or class.
3. Using template with splitted declaration and definition.

For the first scenario, in general, people will not forget to define a member function after the declaration. So this kind of link erro seldom appears. But sometimes people may have declared the constructor but forget the definition and think the system will do it automatically. It is right that system may creat the default constructor if you don't declare it. But this doesn't work when the declaration has been made.

For the second case, if a class has a declared static member, because you cannot initialize it inside the class body (it belongs to the class, not the object), so if you forget defining this static member outside the body, the link error will also occur.

The thrid one is the well-known fallacy: templates behave similarly as ordinary classes and functions with respect to the separation between their declarations and definitions. This is not true. The reason is that a template actually is NOT real code. A good anologic example is: the MS-Word has some letter templates, but they are not real letters until you fill them out. Therefore, one thing we need to remember: templated functions and classes don't actually exist until you use them! A solution is: a header file including template declarations also constains their definitions. However, this seems to violate the rule of the separation of the declaration and definition.

Reference:
[1] http://www.informit.com/guides/content.asp?g=cplusplus&seqNum=298&rl=1

4 comments:

Anonymous said...

Hello. Great job. I did not expect this on a Wednesday. This is a great story. Thanks!

Anonymous said...

Hi all, here every person is sharing these kinds of
know-how, thus it's fastidious to read this weblog, and I used to pay a quick visit this web site everyday.

my web blog ... legal ecstasy

Anonymous said...

I don't even understand how I finished up here, however I believed this post used to be great. I do not recognize who you are but definitely you'гe going to a well-knοwn blоgger ωhen уou aren't already. Cheers!

Feel free to surf to my site; cheap party pills

Anonymous said...

smokeless cigarettes, electronic cigarettes, electronic cigarette, ecigarette, ecig, e cigarette reviews