st-man
July 29th, 2005, 02:35 AM
Hello,
I'am trying to write my own vector class. It is implemented using templates and operator overloading.
In ordern to allow vector addition this global method is implemented
template<typename T>
CVector3D<T> operator+( const CVector3D<T> &v1, const CVector3D<T> &v2 )
{
return( CVector3D<T>( v1._x + v2._x, v1._y + v2._y, v1._z + v2._z ) );
}
In the class definition I define this method as a friend of it,
template<typename T>
class CVector3D
{
public:
friend CVector3D<T> operator+( const CVector3D<T> &, const CVector3D<T> & );
...
but the linking process fails with
if g++ -DHAVE_CONFIG_H -I. -I/home/sschmidt/Develop/vectors/src -I.. -O0 -g3 -MT vectors.o -MD -MP -MF ".deps/vectors.Tpo" -c -o vectors.o /home/sschmidt/Develop/vectors/src/vectors.cpp; \
then mv -f ".deps/vectors.Tpo" ".deps/vectors.Po"; else rm -f ".deps/vectors.Tpo"; exit 1; fi
In file included from /home/sschmidt/Develop/vectors/src/vectors.cpp:29:
/home/sschmidt/Develop/vectors/src/cvector3d.h:62: Warnung: »friend«-Deklaration »CVector3D<T> operator+(const CVector3D<T>&, const CVector3D<T>&)« deklariert eine Nicht-Template-Funktion
/home/sschmidt/Develop/vectors/src/cvector3d.h:62: Warnung: (wenn das nicht beabsichtigt war, sollte sicher gestellt werden, dass das Funktions-Template bereits deklariert wurde, und <> hier hinter Funktionsnamen einfügen) -Wno-non-template-friend schaltet diese Warnung aus)
/bin/sh ../libtool --tag=CXX --mode=link g++ -O0 -g3 -o vectors vectors.o
g++ -O0 -g3 -o vectors vectors.o
vectors.o(.text+0x1b5): In function `main':
/home/sschmidt/Develop/vectors/src/vectors.cpp:49: undefined reference to `operator+(CVector3D<float> const&, CVector3D<float> const&)'
collect2: ld returned 1 exit status
This is probably related to an earlier warning
Warning: »friend« declaration »CVector3D<T> operator+(const CVector3D<T>&, const CVector3D<T>&)« declares a non-template-function
Warning: (if this was not intended you should make sure the function template is already declared and add <>
after the name of the function ) -Wno-non-template-friend switches this warning off
Adding
class CVector3D<T>;
CVector3D<T> operator+( const CVector3D<T> &, const CVector3D<T> & );
before the class definition in order to declare the method does not help either.
Does anyone knows what to do?
The following should work:
CVector3D<float> v1, v2, v3;
...
v3 = v1 + v2;
Kind regards,
Sven Schmidt
I'am trying to write my own vector class. It is implemented using templates and operator overloading.
In ordern to allow vector addition this global method is implemented
template<typename T>
CVector3D<T> operator+( const CVector3D<T> &v1, const CVector3D<T> &v2 )
{
return( CVector3D<T>( v1._x + v2._x, v1._y + v2._y, v1._z + v2._z ) );
}
In the class definition I define this method as a friend of it,
template<typename T>
class CVector3D
{
public:
friend CVector3D<T> operator+( const CVector3D<T> &, const CVector3D<T> & );
...
but the linking process fails with
if g++ -DHAVE_CONFIG_H -I. -I/home/sschmidt/Develop/vectors/src -I.. -O0 -g3 -MT vectors.o -MD -MP -MF ".deps/vectors.Tpo" -c -o vectors.o /home/sschmidt/Develop/vectors/src/vectors.cpp; \
then mv -f ".deps/vectors.Tpo" ".deps/vectors.Po"; else rm -f ".deps/vectors.Tpo"; exit 1; fi
In file included from /home/sschmidt/Develop/vectors/src/vectors.cpp:29:
/home/sschmidt/Develop/vectors/src/cvector3d.h:62: Warnung: »friend«-Deklaration »CVector3D<T> operator+(const CVector3D<T>&, const CVector3D<T>&)« deklariert eine Nicht-Template-Funktion
/home/sschmidt/Develop/vectors/src/cvector3d.h:62: Warnung: (wenn das nicht beabsichtigt war, sollte sicher gestellt werden, dass das Funktions-Template bereits deklariert wurde, und <> hier hinter Funktionsnamen einfügen) -Wno-non-template-friend schaltet diese Warnung aus)
/bin/sh ../libtool --tag=CXX --mode=link g++ -O0 -g3 -o vectors vectors.o
g++ -O0 -g3 -o vectors vectors.o
vectors.o(.text+0x1b5): In function `main':
/home/sschmidt/Develop/vectors/src/vectors.cpp:49: undefined reference to `operator+(CVector3D<float> const&, CVector3D<float> const&)'
collect2: ld returned 1 exit status
This is probably related to an earlier warning
Warning: »friend« declaration »CVector3D<T> operator+(const CVector3D<T>&, const CVector3D<T>&)« declares a non-template-function
Warning: (if this was not intended you should make sure the function template is already declared and add <>
after the name of the function ) -Wno-non-template-friend switches this warning off
Adding
class CVector3D<T>;
CVector3D<T> operator+( const CVector3D<T> &, const CVector3D<T> & );
before the class definition in order to declare the method does not help either.
Does anyone knows what to do?
The following should work:
CVector3D<float> v1, v2, v3;
...
v3 = v1 + v2;
Kind regards,
Sven Schmidt