bug-report: operator overloading in template classes

Thorsten Holtkaemper thorsten.holtkaemper@gmd.de
Mon Sep 11 06:55:00 GMT 2000


Hi,
I think I've found a bug in the gcc C++ compiler concerning operator
overloading in template classes.

Description:
Overloading the +operator (for example) in a template class in two ways,
one as a member function and one as friend function (of course with
different arguments), doesn't compile, if I define the functions outside
the template declaration. It works, if I define the functions inside the
template declaration.

A corresponding .ii file is attached.

Compile output (with version information) is here:

g++ -v --save-temps mathtest.C -o mathtest

Reading specs from /usr/lib/gcc-lib/i486-suse-linux/2.95.2/specs
gcc version 2.95.2 19991024 (release)
 /usr/lib/gcc-lib/i486-suse-linux/2.95.2/cpp -lang-c++ -v -D__GNUC__=2
-D__GNUG__=2 -D__GNUC_MINOR__=95 -D__cplusplus -D__ELF__ -Dunix
-D__i386__ -Dlinux -D__ELF__ -D__unix__ -D__i386__ -D__linux__ -D__unix
-D__linux -Asystem(posix) -D__EXCEPTIONS -Acpu(i386) -Amachine(i386)
-Di386 -D__i386 -D__i386__ -Di486 -D__i486 -D__i486__ mathtest.C
mathtest.ii
GNU CPP version 2.95.2 19991024 (release) (i386 Linux/ELF)
#include "..." search starts here:
#include <...> search starts here:
 /usr/include/g++
 /usr/local/include
 /usr/lib/gcc-lib/i486-suse-linux/2.95.2/include
 /usr/include
End of search list.
The following default directories have been omitted from the search
path:

/usr/lib/gcc-lib/i486-suse-linux/2.95.2/../../../../i486-suse-linux/include
End of omitted list.
 /usr/lib/gcc-lib/i486-suse-linux/2.95.2/cc1plus mathtest.ii -quiet
-dumpbase mathtest.cc -version -o mathtest.s
GNU C++ version 2.95.2 19991024 (release) (i486-suse-linux) compiled by
GNU C version 2.95.2 19991024 (release).
paMath.H: In instantiation of `paVec3<double>':
mathtest.C:6:   instantiated from here
paMath.H:15: invalid use of undefined type `class paVec3<double>'
paMath.H:16: forward declaration of `class paVec3<double>'
paMath.H:15: confused by earlier errors, bailing out


I hope it will help you!
Best regards and thanks for all your work,
Thorsten Holtkaemper.
# 1 "mathtest.C"
# 1 "paMath.H" 1




template<class C> class paVec3;
template<class C> const paVec3<C> operator+(const C&, const paVec3<C>&);


template<class C> class paVec3
{
	private:
		C vdata[3];
	public:
		const paVec3<C> operator+(const C&) const;
		friend const paVec3<C> operator+ <> (const C&, const paVec3<C>&);
};



template<class C>
inline const paVec3<C> paVec3<C>::operator+(const C& right) const
{
	return paVec3<C>(vdata[0]+right, vdata[1]+right, vdata[2]+right);
}   


template<class C>
inline const paVec3<C> operator+(const C& left, const paVec3<C>& right)
{
	return paVec3<C>(left + right.vdata[0],
			 left + right.vdata[1],
			 left + right.vdata[2]);
}



# 1 "mathtest.C" 2


int main(void)
{
	
 	paVec3<double> v;
	
	v + 1.;
	1. + v;
	
	return 0;
}


More information about the Gcc-bugs mailing list