 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Patrick Labatut Guest
|
Posted: Tue Sep 27, 2005 3:02 pm Post subject: Templates and friend operator |
|
|
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
|
Posted: Tue Sep 27, 2005 8:44 pm Post subject: Re: Templates and friend operator |
|
|
Hi Patrick,
could you send your error messages too? I'm having a
pretty much similar problem.
Micha
|
|
| Back to top |
|
 |
Patrick Labatut Guest
|
Posted: Wed Sep 28, 2005 8:17 am Post subject: Re: Templates and friend operator |
|
|
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
|
Posted: Wed Sep 28, 2005 9:29 am Post subject: Re: Templates and friend operator |
|
|
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 |
|
 |
|
|
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
|
|