473,756 Members | 3,051 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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,con st P<T>&);

template <class T>
class P{
private:
T d;
public:
P(int i=0);
P<T> operator*(T);
friend P<T> operator*<>(T,c onst 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,c onst 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 1822
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,con st P<T>&);

template <class T>
class P{
private:
T d;
public:
P(int i=0);
P<T> operator*(T);
friend P<T> operator*<>(T,c onst 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,c onst 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,con st P<T>&);

template <class T>
class P{
private:
T d;
public:
P(int i=0);
P<T> operator*(T);
friend P<T> operator*<>(T,c onst 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
1842
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 (TESTCODE 2) before calling the subroutine "testmail2", I would never get the expected result. I would appreciate if anyone would be so kind to help me solve the problem or suggest an alternative solutions. My objective is to create mutiple child...
11
2905
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 fine. void Macsock::writeData(char *buffer, int fd) { char message = "This is a message from the front."; if(write(fd, message, sizeof(message)) < 0 )
8
9855
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 | ios::binary) to declare a file object with read/write modes turned on for working with binary data. I've tried this and my file is not created. The only time it is created is when I specify ifstream or ofstream but not fstream. I've tried removing the...
3
1932
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 run into a problem before I even start! The script works OK, it runs the image in from the left hand side quite nicely, great. Problem I have it always scrolls up to the top of the page, when the link I click maybe 2 or 3 screens below (does that...
3
2323
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 on it and choose Open from the context menu, I want the form to reappear. For that, I got the point covered. Even when the form is minimize, the behavior is like MSN Messenger. But one problem arose. When I close the form (the first time), it...
14
13814
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*/ printf("%c\n",a);
7
8112
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 MDA exception i.e. The CLR has been unable to transition from COM context 0x1a0768 to COM context 0x1a08d8 for 60 seconds. The thread that owns the destination context/apartment is most likely either doing a non pumping wait or processing a...
3
3447
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 got no reply, in an OpenGL group somebody recommended me to use Visual C++ which I did. That worked OK but I do would like to use Watcom. In the meantime I found solutions to several of the errors I got but one is left which I cannot find a...
5
5524
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
3317
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 in red and deletions in red strike though. There seem to be several in Perl and Python and many diff programs which all seem to be line based and work on text written in PHP.
0
9273
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10032
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9872
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9841
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9711
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7244
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6534
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3805
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2666
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.