Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Problem with member and non-member binary operator in template class

Reply
Thread Tools

Problem with member and non-member binary operator in template class

 
 
ghager
Guest
Posts: n/a
 
      01-16-2006
Hi all,

I must be blind or stupid. Please consider the following code:

----
....
template <class T> class P;
template <class T> P<T> operator*(T,const P<T>&);

template <class T>
class P{
private:
T d;
public:
P(int i=0);
P<T> operator*(T);
friend P<T> operator*<>(T,const P<T>&); // this is line 15
};

template <class T> P<T> operator*(T x,const P<T>& p)
{
return P<T>(x*p.d);
}

template <class T>
P<T>:(int i) : d(i) {}

template <class T>
P<T> P<T>:perator*(T x)
{
return P<T>(x*d);
}

int main()
{
P<int> p(4),q,r;
q=3*p;
r=p*3;
return 0;
}
----

Compiling this code, e.g. g++ 4.0 says:

opp.cc:15: error: declaration of 'operator*' as non-function
opp.cc:15: error: expected ';' before '<' token

Intel 9.0 says:

opp.cc(15): error: function "P<T>:perator* [with T=int]" is not a
template
friend P<T> operator*<>(T,const P<T>&);


Mysteriously, when I interchange the friend declaration in P
with the declaration of the member operator*, everything
is fine. What's going on?

Thanks for any help,
bye,
Georg.

 
Reply With Quote
 
 
 
 
Carlos Martinez Garcia
Guest
Posts: n/a
 
      01-16-2006
I'm not an expert, but I think if operator is friend, it must not belong
to P when you define it (it must be operator*, and not P<T>:perator*)


ghager wrote:
> Hi all,
>
> I must be blind or stupid. Please consider the following code:
>
> ----
> ...
> template <class T> class P;
> template <class T> P<T> operator*(T,const P<T>&);
>
> template <class T>
> class P{
> private:
> T d;
> public:
> P(int i=0);
> P<T> operator*(T);
> friend P<T> operator*<>(T,const P<T>&); // this is line 15
> };
>
> template <class T> P<T> operator*(T x,const P<T>& p)
> {
> return P<T>(x*p.d);
> }
>
> template <class T>
> P<T>:(int i) : d(i) {}
>
> template <class T>
> P<T> P<T>:perator*(T x)
> {
> return P<T>(x*d);
> }
>
> int main()
> {
> P<int> p(4),q,r;
> q=3*p;
> r=p*3;
> return 0;
> }
> ----
>
> Compiling this code, e.g. g++ 4.0 says:
>
> opp.cc:15: error: declaration of 'operator*' as non-function
> opp.cc:15: error: expected ';' before '<' token
>
> Intel 9.0 says:
>
> opp.cc(15): error: function "P<T>:perator* [with T=int]" is not a
> template
> friend P<T> operator*<>(T,const P<T>&);
>
>
> Mysteriously, when I interchange the friend declaration in P
> with the declaration of the member operator*, everything
> is fine. What's going on?
>
> Thanks for any help,
> bye,
> Georg.
>

 
Reply With Quote
 
 
 
 
ghager
Guest
Posts: n/a
 
      01-16-2006

Carlos Martinez Garcia wrote:
> I'm not an expert, but I think if operator is friend, it must not belong
> to P when you define it (it must be operator*, and not P<T>:perator*)


In fact I have two operator* functions, one is a member with
one argument and one is a non-member with two arguments (the
original class type as second argument). So the compiler should know
when to select which, but obviously things get mixed up.

 
Reply With Quote
 
Carlos Martinez Garcia
Guest
Posts: n/a
 
      01-18-2006
ghager wrote:
> Carlos Martinez Garcia wrote:
>
>>I'm not an expert, but I think if operator is friend, it must not belong
>> to P when you define it (it must be operator*, and not P<T>:perator*)

>
>
> In fact I have two operator* functions, one is a member with
> one argument and one is a non-member with two arguments (the
> original class type as second argument). So the compiler should know
> when to select which, but obviously things get mixed up.
>

Yes. You're right. I haven't see the second (non-member operator*)
I test it with g++ 3.3.6 and compiles ok

I'm sorry
 
Reply With Quote
 
ghager
Guest
Posts: n/a
 
      01-18-2006

> Yes. You're right. I haven't see the second (non-member operator*)
> I test it with g++ 3.3.6 and compiles ok


Yes, older compilers like gcc 3.3.X and SUN WS6 accept this code.
But all newer ones (PGI 6.0, Intel 9.1beta, gcc 4.0) generate
an error.

Another piece of information: If the class isn't a template, it works
fine.

Clueless ,
Georg.

 
Reply With Quote
 
Thomas Tutone
Guest
Posts: n/a
 
      01-18-2006
ghager wrote:
> Hi all,
>
> I must be blind or stupid. Please consider the following code:
>
> template <class T> class P;
> template <class T> P<T> operator*(T,const P<T>&);
>
> template <class T>
> class P{
> private:
> T d;
> public:
> P(int i=0);
> P<T> operator*(T);
> friend P<T> operator*<>(T,const P<T>&); // this is line 15


I think you mean:

friend P<T> operator*(T, constP<T>&);

> };
>
> template <class T> P<T> operator*(T x,const P<T>& p)
> {
> return P<T>(x*p.d);
> }
>
> template <class T>
> P<T>:(int i) : d(i) {}
>
> template <class T>
> P<T> P<T>:perator*(T x)
> {
> return P<T>(x*d);
> }
>
> int main()
> {
> P<int> p(4),q,r;
> q=3*p;
> r=p*3;
> return 0;
> }
> ----


Best regards,

Tom

 
Reply With Quote
 
ghager
Guest
Posts: n/a
 
      01-19-2006
No, this is a friend function declaration in which the
friend is an appropriately typed function which
is not a template specialization (C++ standard
14.5.3).

What I want to have is a friend that is an appropriately
typed function template specialization for each specialization
of the class template.

Bye,
Georg.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to use the template member function of a template in the memberfunction of another template class? Peng Yu C++ 3 10-26-2008 03:51 PM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
parse error in gcc but success in vc.net, call a non_template class's template member function from a template class's member function! ken C++ 2 06-28-2005 06:57 AM
A parameterized class (i.e. template class / class template) is not a class? christopher diggins C++ 16 05-04-2005 12:26 AM
Class Template Member Class Template Specialisaton Query Simon G Best C++ 2 09-25-2003 03:45 PM



Advertisments