Linker error with template function

G

giganut

Hi,
I'm using MS VS.Net2003, and I'm getting a linker error in a project for a
templated friend function

Specifically:

template <class T> class Vec3

{...

friend Vec3<T> operator -( const Vec3<T>& v );

};

Then later I have defined:

template <class T> inline Vec3<T> operator -(const Vec3<T>& v)

{

return Vec3<T>( -v.n[0], -v.n[1], -v.n[2] );

}



and explicitly instantiated:

typedef Vec3<double> Vec3d;

But, then I get a linker error:

error LNK2019: unresolved external symbol "class Vec3<double> __cdecl
operator-(class Vec3<double> const &)" (??G@YA?AV?$Vec3@N@@ABV0@@Z)
referenced in function "bool __cdecl pointToTriDist(class Vec3<double>,class
Vec3<double> *,double,double &,class Vec3<double> &,class Vec3<double> &,int
*)" (?pointToTriDist@@YA_NV?$Vec3@N@@PAV1@NAANAAV1@3PAH@Z)


This happens for all the templated friend functions (global) defined in the
template classes. What is the cause for this ? How can I fix this ?



Thanks,

-g
 
A

amparikh

giganut said:
Hi,
I'm using MS VS.Net2003, and I'm getting a linker error in a project for a
templated friend function

Specifically:

template <class T> class Vec3

{...

friend Vec3<T> operator -( const Vec3<T>& v );

};

Then later I have defined:

template <class T> inline Vec3<T> operator -(const Vec3<T>& v)

{

return Vec3<T>( -v.n[0], -v.n[1], -v.n[2] );

}



and explicitly instantiated:

typedef Vec3<double> Vec3d;

But, then I get a linker error:

error LNK2019: unresolved external symbol "class Vec3<double> __cdecl
operator-(class Vec3<double> const &)" (??G@YA?AV?$Vec3@N@@ABV0@@Z)
referenced in function "bool __cdecl pointToTriDist(class Vec3<double>,class
Vec3<double> *,double,double &,class Vec3<double> &,class Vec3<double> &,int
*)" (?pointToTriDist@@YA_NV?$Vec3@N@@PAV1@NAANAAV1@3PAH@Z)


This happens for all the templated friend functions (global) defined in the
template classes. What is the cause for this ? How can I fix this ?



Thanks,

-g

this is discussed many times before...check out the FAQ...
http://www.parashift.com/c++-faq-lite/templates.html#faq-35.16
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top