Skip to main content
gamedev.net
🔒 Locked

C++ Template Programming Question

Started by Zerox Millienium Aug 21, 2006 at 4:52 PM 3 replies 750+ views
Original Post
Zerox Millienium
Zerox Millienium
I have the following code samples that I have written deliberately omitting the mention of header files that I have included, // helper.h #ifndef _HELPER_H_ #define _HELPER_H_ namespace CppTemplate001 { class Helper { public: Helper(); ~Helper(); template static T compare(T a, T b); private: }; } #endif // _HELPER_H_ // helper.cpp Helper::Helper() { } Helper::~Helper() { } template T Helper::compare(T a, T b) { if (a < b) return a; return b; } // main.cpp int main(int argc, char **argv) { int a = 10; int b = 50; int c = 0; c = Helper::compare(a, b); cout << c << endl; return EXIT_SUCCESS; } I wanted to achieve something like having static helper methods to be housed in a class but not making the class generic, I wanted to define different generic templates for certain methods and not all of them. Is it possible to do this? The code compiles well before I called Helper::compare() from main.cpp. When I include that line otherwise, it fails to compile giving me a linker error not able to resolve the symbol. So I can't do it this way, can I?
Paulius Maruska
Paulius Maruska
templates must be defined in header files - you cant declare them in header and then define them in cpp files.
mikeman
mikeman
This has to do with how templates work. Consider the following:

template
T Helper::compare(T a, T b)
{
if (a < b) return a;
return b;
}


Now, when the compiler sees the following in main.cpp:
int a = 10;int b = 50;
Helper::compare(a, b);

generates a function(without you being aware of it ofcourse):

int Helper::compare(int a,int b)
{
if (a < b) return a;
return b;
}

if, further along the code, sees:
string sa="Hello";string sb="World";
Helper::compare(sa,sb);

generates another function:

string Helper::compare(string a,string b)
{
if (a < b) return a;
return b;
}

When all needed functions have been generated inside main.cpp, it is finally compiled.

In order to do all that, the compiler must know the implementation of the templated function. Since the compilation of .cpp files is done individually, and since you have put the implementation in helper.cpp, it has no way of knowing that when it compiles main.cpp . In other words, the easiest way to fix this would be to put both the declaration and the definition inside the helper.h.


Zerox Millienium
Zerox Millienium
Cool, thanks guys, I managed to get it work now.

I've create a helper.inl to put the implementation inside and have it included as part of the helper.h header file.

I still want to hide the implementation some how, very much like creating inline functions hehz.

Perfect. Thanks again.
Skarsha
Skarsha
You can implement templates in source code and call them from other files.

You just need to use the explicit template specifier:
template<>


// Aside:
by the way, when quoting source code, use [sou_rce] and [/sou_rce] (replace sou_rce with source) tags and you get:

int someSauce() {    // indentation!}



// Back to the Topic:

// Helper.cpp
template<>int Helper::compare(int a, int b) {    return 1336;}template<>char Helper::compare(char a, char b) {    return 'Z';}


Try the above. I'm killing time at work and don't have a compiler to test it on sorry. - You may have to declare the functions before defining them, which might not make this very useful. You might also need typename before the "int Helper" or "char Helper". Can't remember right now.



Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.