Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Declaring friend overloaded operators in templates

Posted on 2003-11-06
10
350 Views
Last Modified: 2010-04-01
Hi, everyone,

I'm compiling the following code with VC++ SP5, and running into strange errors:

(line 5 is the one with the friend operator* declaration)
-------------------------------------------------------
template< class T > class aa
{
public:
      T a;
      friend aa<T> operator*<> ( const aa<T>&, const aa<T>& );
};

template< class T> aa<T> operator* ( const aa<T>& i, const aa<T> & j)
{
      return i*j;
}

int main()
{
      return 0;
}
---------------------------------------------------------------
This compiles fine on MinGW.


I have STLPort installed, but I've moved the directories well down, so they should not create problems.
Errors in VC++ are:
###########
new.cxx(5) : error C2143: syntax error : missing ';' before '<'
see reference to class template instantiation 'aa<T>' being compiled
###########
After this, there are more errors as cl assumes that the friend being declared is data, rather than function, and so on.

My question: What am I doing wrong?
0
Comment
Question by:narkie
10 Comments
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 9699609
>>>>>>   friend aa<T> operator*<> ( const aa<T>&, const aa<T>& );

What do u mean by putting <> after *

0
 

Author Comment

by:narkie
ID: 9699668
http://gcc.gnu.org/faq.html#friend

To quote:

"the angle brackets must be present, otherwise the declaration will be taken as a non-template function"
0
 
LVL 10

Accepted Solution

by:
Sys_Prog earned 130 total points
ID: 9700046
Hi narkie

I am not very sure, but that may be specifc to compilers

refer the following link for the same

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccelng/htm/tions_31.asp

HTH
Amit
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 10

Expert Comment

by:Sys_Prog
ID: 9700051
I think removing the <> after * would do the job

I would have tried it but I do not currently have access to a VC compiler.

HTH

Amit
0
 
LVL 13

Expert Comment

by:SteH
ID: 9700057
looking at the faq you posted the function has to be forward declared:

template <class T>
class aa;

template <class T>
aa<T> opertor*<> (const aa<T>&, const aa<T>&);

template <class T> class aa {
....
}

before defining it.
0
 

Author Comment

by:narkie
ID: 9700566
Sorry for being so late guys (arrg. sleep :().

@SteH:
Doing a declaration as you rightly noticed (my mistake - didn't read the FAQ properly) merely transfers the error from the friend declaration to the first declaration. MinGW seems to handle it fine, though. I wonder what's happening...

@Sys_Prog:
Removing the  angle brackets makes it a template class with a non-template function declaration. Strange thing is, if I do that, and instantiate two objects of class aa<int> and try to multiply them, cl compiles them happily. gcc (rightly, I think) starts yelling that there is no operator*( const aa<int>&, const aa<int>& ). I'm confused.

Thanks for the help!
0
 
LVL 9

Expert Comment

by:_ys_
ID: 9702583
Slight adjustment to what SteH posted.

template <class T>
class aa;

template <class T>
aa<T> opertor* (const aa<T>&, const aa<T>&);  // *** no <>

template <class T> class aa {
...
    friend template <class T>
    aa<T> opertor*<> (const aa<T>&, const aa<T>&);
...
}
0
 
LVL 15

Expert Comment

by:efn
ID: 9702836
For what it's worth, I think I dealt with a similar problem a few years ago and came to the conclusion that there was no formulation that would compile in both gcc and Visual C++.  I ended up with compiler-specific code selected with #ifdefs.

--efn
0
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 9705250
I agree woth efn

That's what the link in my previous post says
0
 

Author Comment

by:narkie
ID: 9705850
Ah well, thank you.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question