C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Templates and friend operator

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Patrick Labatut
Guest





PostPosted: Tue Sep 27, 2005 3:02 pm    Post subject: Templates and friend operator Reply with quote



The following code should compile fine with g++ 3.3 and 3.4. However if
I instead put the friend operator* declaration right below the operator*
member, g++ 3.4 won't compile it anymore.

Could someone give me some explanation on this weird behaviour ?

/*************************************************************************/

template<class FloatT>
class Vector3;

template<class FloatT>
Vector3<FloatT> operator*(FloatT f, const Vector3<FloatT>& v);

template<class FloatT>
class Vector3 {
public:
Vector3(FloatT x, FloatT y, FloatT z)
{
this->v[0] = x;
this->v[1] = y;
this->v[2] = z;
}

friend Vector3<FloatT> operator*<>(FloatT f, const
Vector3<FloatT>& v);

FloatT operator*(const Vector3<FloatT>& v) const
{
return (this->v[0] * v.v[0]
+ this->v[1] * v.v[1]
+ this->v[2] * v.v[2]);
}

private:
FloatT v[3];
};

template<class FloatT>
Vector3<FloatT> operator*(FloatT f, const Vector3<FloatT>& v)
{
return Vector3<FloatT>(f * v.v[0], f * v.v[1], f * v.v[2]);
}

int main(int argc, char *argv[])
{
Vector3<float> v(0, 0, 0), w(0, 0, 0);

w = 2.0f * v;

return 0;
}
Back to top
Micha
Guest





PostPosted: Tue Sep 27, 2005 8:44 pm    Post subject: Re: Templates and friend operator Reply with quote



Hi Patrick,

could you send your error messages too? I'm having a
pretty much similar problem.

Micha

Back to top
Patrick Labatut
Guest





PostPosted: Wed Sep 28, 2005 8:17 am    Post subject: Re: Templates and friend operator Reply with quote



On 2005-09-27, Micha <michael.andrassy (AT) gmail (DOT) com> wrote:
Quote:
could you send your error messages too?

yoohoo.cpp:24: error: declaration of `operator*' as non-function
yoohoo.cpp:24: error: expected `;' before '<' token
yoohoo.cpp: In function `Vector3 Vector3<FloatT>&) [with FloatT = float]':
yoohoo.cpp:40: instantiated from here
yoohoo.cpp:27: error: `float Vector3<float>::v[3]' is private
yoohoo.cpp:33: error: within this context
yoohoo.cpp:27: error: `float Vector3<float>::v[3]' is private
yoohoo.cpp:33: error: within this context
yoohoo.cpp:27: error: `float Vector3<float>::v[3]' is private
yoohoo.cpp:33: error: within this context

Some people I already asked told me they were getting similar error
messages with other compilers than g++ (however I don't know which
compilers they tried).

Back to top
Marc
Guest





PostPosted: Wed Sep 28, 2005 9:29 am    Post subject: Re: Templates and friend operator Reply with quote

Patrick Labatut wrote:

Quote:
Some people I already asked told me they were getting similar error
messages with other compilers than g++ (however I don't know which
compilers they tried).

Sun Studio:

Warning: A friend function with template-id name must have a template
declaration in the nearest namespace.
Where: While specializing "Vector3<float>".
Where: Specialized in non-template code.

Comeau:

error: function "Vector3<FloatT>::operator*" is not a template
friend Vector3<FloatT> operator*<>(FloatT f, const Vector3<FloatT>& v);
^

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.