C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Simple question (namespaces and operators)

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Marco T.
Guest





PostPosted: Wed Aug 09, 2006 9:10 am    Post subject: Simple question (namespaces and operators) Reply with quote



Hi all,

why the former code fragment works and the latter doesn't?

=============
//CORRECT CODE:

namespace ciao
{
class A{};
A operator *(A &a, A &b) {return a;};
A method() ;
}
using namespace ciao;
A method() {A a1,a2; return a1*a2;}
=============
//INCORRECT CODE

namespace ciao
{
class A{};
A operator *(A &a, A &b);
A method() ;
}
using namespace ciao;
A operator *(A &a, A &b) {return a;}
A method() {A a1,a2; return a1*a2;} //line X

error: 'operator *' is ambiguous at line X
(compiler: VC++ 6.0)

=============
The issue is that I don't want to write the implementation of operator
* inside the namespace declaration.

thanks
marco
Back to top
Marco T.
Guest





PostPosted: Wed Aug 09, 2006 9:10 am    Post subject: Re: Simple question (namespaces and operators) Reply with quote



Sumit RAJAN wrote:

Quote:
A ciao::operator *(A &a, A &b) {return a;}

I would not recommend the using directive. Further, it may be a nice
idea to make the parameters const refs.

A method() {A a1,a2; return a1*a2;} //line X

Regards,
Sumit.

Thanks!

:-)
Back to top
Sumit RAJAN
Guest





PostPosted: Wed Aug 09, 2006 9:10 am    Post subject: Re: Simple question (namespaces and operators) Reply with quote



Marco T. wrote:

Quote:
//INCORRECT CODE

namespace ciao
{
class A{};
A operator *(A &a, A &b);
A method() ;
}
using namespace ciao;
A operator *(A &a, A &b) {return a;}

A ciao::operator *(A &a, A &b) {return a;}

I would not recommend the using directive. Further, it may be a nice
idea to make the parameters const refs.

Quote:
A method() {A a1,a2; return a1*a2;} //line X

Regards,
Sumit.
Back to top
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
 


Powered by phpBB © 2001, 2006 phpBB Group