narkie
asked on
Declaring friend overloaded operators in templates
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?
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?
ASKER
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"
To quote:
"the angle brackets must be present, otherwise the declaration will be taken as a non-template function"
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
I would have tried it but I do not currently have access to a VC compiler.
HTH
Amit
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.
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.
ASKER
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!
@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!
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>&);
...
}
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>&);
...
}
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
--efn
I agree woth efn
That's what the link in my previous post says
That's what the link in my previous post says
ASKER
Ah well, thank you.
What do u mean by putting <> after *