473,322 Members | 1,699 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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

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>::P(int i) : d(i) {}

template <class T>
P<T> P<T>::operator*(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>::operator* [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.

Jan 16 '06 #1
6 1804
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>::operator*)
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>::P(int i) : d(i) {}

template <class T>
P<T> P<T>::operator*(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>::operator* [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.

Jan 16 '06 #2

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>::operator*)


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.

Jan 16 '06 #3
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>::operator*)

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
Jan 18 '06 #4
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.

Jan 18 '06 #5
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>::P(int i) : d(i) {}

template <class T>
P<T> P<T>::operator*(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

Jan 18 '06 #6
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.

Jan 19 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Michael | last post by:
Hi, thank you for taking your previous time to read the following problem that I face. I'm trying to create multiple child process and TESTCODE 1 work fine, however if I try to connect to MYSQL...
11
by: Jeff Crouse | last post by:
In my project, I have successfully opened a sock_stream style socket connection between two processes and am trying to send 'buffer' to a connected client, 'fd'. The following code works just...
8
by: Brandon McCombs | last post by:
This may be the wrong group but I didn't see anything for VC++ so I'm trying here. I have a C++ book by Deitel and Deitel that says I can use fstream File("data.dat", ios::in | ios::out |...
3
by: Chris Leonard | last post by:
Hi. I've copied some code from a book which will enable me to make a layer around a page, my aim is to do something a little more complex but this I thought would get me started. Anyway, I've...
3
by: ThunderMusic | last post by:
Hi, I'm trying to have a MSN Messenger like form/app closing behavior. When I click on the X button, I only want the form to disappear and when I double-click on the notify icon or right-click...
14
by: main() | last post by:
I know this is the problem that most newbies get into. #include<stdio.h> int main(void) { char a; scanf("%c",&a); /*1st scanf */ printf("%c\n",a); scanf("%c",&a); /*2nd scanf*/...
7
by: Kalpana | last post by:
Hi, As iam working on convertion of C#1.1 to 2.0. Iam facing a problem (screen is flickered) while opening and closing any artifacts. When i tried to debug it is througing ContextSwitchDeadlock...
3
by: Rene | last post by:
Hello to all! For a long time I have been "fighting" a problem compiling an OpenGL program which uses GLUT. First I have put a question in a Watcom group (I want to use this compiler) to which I...
5
by: Mike | last post by:
I use MS SQL EXPRESS DB VS 2005, c# Win Application I have problem "The string is non-numeric" with formula CDbl({pr_DajPonudu;1.KolicinskiPopust})/100 * {@NajamProstora}
6
by: Aaron Gray | last post by:
Hi, I am working on an HTML WYSISYG Wiki and need to display a diff page like WikiPedia does if two people edit a file at the same time to give the second user the diff. Basically with additions...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.