Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

difficulty with template class methods in VC++ 6

Status
Not open for further replies.

Chromebender

Programmer
Jul 16, 2004
4
US
First, I scanned this forum for info on this problem, and while I am now aware of a known issue with VC++ and template functions with template parameters that do not appear as function arguments, this does not appear to be the same bug (if it is, please explain how to fix it). AFAIK, programs with that bug will compile and run but only use the first instantiation of a function, which isn't my problem.

Briefly, I have a CVector class that looks like this:

template <typename Type, int length> class CVector
{
public:
void Normalize()
{
int nIndex;
Type norm=Norm();
for (nIndex=0;nIndex<m_ArrayLength;nIndex++)
m_TypeArray[0] /= norm;
};
...

now if I compile my program with this header file "linear.h"
everything works fine. But I don't want to put the implementation in the header file, I want it in a .CPP file.

However, when I remove the function body of Normalize() from the class definition and place it in "linear.cpp" like so:

template <typename Type, int length> void CVector<Type,length>::Normalize()
{
int nIndex;
Type norm =Norm();
for (nIndex=0;nIndex<m_ArrayLength;nIndex++)
m_TypeArray[0] /= norm;
}

VC++ gives me the error:
error LNK2001: unresolved external symbol "public: void __thiscall CVector<double,3>::Normalize(void)" (?Normalize@?$CVector@N$02@@QAEXXZ)

I tried including dummy parameters
Normalize(Type *dummy=0, int dummy2=0) but while it compiles, it gives the same linker error. What kills me is the method works fine as long as I include ALL the code in the class definition in the header file.

Anyone?
 
Add this line to the top of your cpp file:

template class CVector<double, 3>;

You'll get a warning, but it should compile now.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top