Advertisement

Templates?

Started by February 03, 2000 05:06 PM
5 comments, last by Jesse Chounard 24 years, 8 months ago
I''m attempting to put together a simple templated linked list, and running into linker errors. I''m guessing that I''m missing something simple, because I''ve done this before, but I can''t seem to see it. Any help would be greatly appreciated. The files (3 of them, pretty small), are available here: http://www.umr.edu/~jessec/templates/ list.h and list.cpp are the list class files. main.cpp is just a main(), so I could test. Oddly enough, if I move the main() to the list.cpp file, it works. I tried it with Visual C++ 6.0, DJGPP, and gcc, so I''m sure it''s something I''m doing wrong. Again, thanks for any help. Jesse Chounard jessec@umr.edu
I believe you need to have the template functions in the .h file, not in a separate .cpp file. It''s worth a shot anyway.
Advertisement
Yes, I had the same problem. You are going to have to implement the functions in the same h file that you declare them in, not in a cpp file.

Some compilers handle template differently than others.

The common practice I''ve seen with templates is to place
the function implementations in a .cc file and include
this at the end of your .h file. Then you''ve still got
a familiar separation between interface and implementation
and if you move to a compiler that handles them in a
different manner you don''t have go through and break
up all your .h''s into two files.

Thanks everybody. Upon moving the functions to the header file, it worked like a charm.


Jesse Chounard
jessec@umr.edu
Templates are nothing but gloified macros. The entire ''thing'' must be defined prior to its use. It is handled at compile time not at link time.


mdp
Advertisement
1) Use STL.

2) Templates are better than macros in that the compiler performs type-checking on them.

Mason McCuskey
Spin Studios - home of Quaternion, 2000 GDC Indie Games Fest Finalist!
www.spin-studios.com
Founder, Cuttlefish Industries
The Cuttlefish Engine lets anyone develop great games for iPad, iPhone, Android, WP7, the web, and more!

This topic is closed to new replies.

Advertisement