The C++Course provides a general introduction to programming in C++. It is based on A.B. Downey's book, How to Think Like a Computer Scientist. Click here for details.


The Pitfalls of Templates

Ok, so now you probably think that templates are the coolest things in the world. There is however, the all too familiar problem of getting the actual code to work. The templated aspects of your function will only work if the type that you are using already has the constructors, operators, and etc. defined. For example, if you were to use the += operator with your typeparamter:

SOMETYPE += x;

However, if the datatype, class, or struct that you use SOMETYPE to represent does not have a += operator defined, then the compiler, specifically, the linker will give you an error and your start losing hair. Don't worry, this is all a part of the sometimes seemingly difficult process of getting templates and the like to work. Have fun.


Last Update: 2005-12-05