link error

Hi, I have a big problem

… I have this code

template <class T>
class CMathParser
{
public:
/* some code */

static CMathParser&lt;T&gt;* Parse(CString& fx);

}

…the problem is that when I call this fx by CMathParser<float>::Parse(fx); it makes thie err -
error LNK2001: unresolved external symbol “public: static class CMathParser<float> * __cdecl CMathParser<float>::Parse(class CString &)” (?Parse@?$CMathParser@M@@SAPAV1@AAVCString@@@Z)

how can I avoid this err??

…I’m using VC++6.0

Help. Thnx

Maybe you need an instance of CMathParser<float> defined somewhere first so that the static function can be instantiated? Maybe an explicit instantiation of the class as follows:

------------------ code --------------------
template <class T>
class CMathParser
{
public:
/* some code */

static CMathParser<T>* Parse(CString& fx);
}

// Explicit instantiation.
template class CMathParser<float>;

------------------ code --------------------

The instantiation can be in the header file where the template is declared/defined or in the implementation file where the class/static function is called. You can probably explicitly instantiate the static function alone if you like:

template static CMathParser<float>* Parse(CString& fx);

Hope these ideas will help. I haven’t tried to compile your examples myself but I have done similar stuff with templates before.

Cheers,
Toby

Here’s what I remember about using template classes. With normal classes, you often do the implementation in the .cpp, and have the definition in the .h. With template classes, however, you need the full implementation defined before you actually implement it. So basically, you need the full class in the .h file or you need to #include the .cpp file before you do your

CMathParser<float> Blah;

I just reread your question again, and I’m not completely sure this’ll fix your problem. I haven’t worked with static functions in template classes before.

Deiussum, that is the issue. The template code needs to be all in the header files. Think about it. It is all logical :

.cpp files are compilation unit, so code in those files get compiled, but a template is not compilable code! It needs a type. So a template needs to have the definition and the implementation in the header file!

but if you specialise you template, then the definitions can all be in a .cpp file, because the type is resolved.

ex :

//…in the header file

//needs to be in header file because
//it is not compilable
template<typename T>
void func( T value )
{
//do stuff
}

//can be implemented in .cpp file
template<>
void func( int value );

//…later in .cpp file
template<>
void func( int value )
{
//do stuff
}

the same thing can be done for classes

So when you create a template object in a compilation unit then, a definition of the class or function is created with the class(es) or type(s) you pass as template params and then compiled.

[This message has been edited by Gorg (edited 02-09-2001).]

[This message has been edited by Gorg (edited 02-09-2001).]