Simple question (namespaces and operators)

M

Marco T.

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
 
S

Sumit RAJAN

Marco said:
//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::eek:perator *(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.
 
M

Marco T.

Sumit said:
A ciao::eek:perator *(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.


Regards,
Sumit.

Thanks!

:)
 
P

peter koch

Marco said:
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() ;

You never implement method().
}
using namespace ciao;
A method() {A a1,a2; return a1*a2;}

This function is new - it is not an implementation of ciao::method.
=============
//INCORRECT CODE

namespace ciao
{
class A{};
A operator *(A &a, A &b);
A method() ;
You never implement operator * and 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

The compiler does not know if it should call ::eek:perator *(A &a, A &b)
or ciao::eek:perator *(A &a, A &b). Both are "perfect" fits and would be
so even without the using directive.
(compiler: VC++ 6.0)

Its to old (and has been for lots of years!). Get a new one from
Microsoft or gcc. Both are nice and free.
=============
The issue is that I don't want to write the implementation of operator
* inside the namespace declaration.

The problem is that you don't understand using directives. The using
namespace ciao directive in your first example only tells the compiler
to look up names in that namespace. The method you declare will be in
the global namespace. To declare the method in the ciao namespace you
must write it "inside" the namespace. E.g.:

A ciao:method() {A a1,a2; return a1*a2;}

-or-

namespace ciao // reopening the namespace
{
A method() {A a1,a2; return a1*a2;}
}

(If the function was more complex, it would normally be placed in a
..cpp file).
So every function must be defined (implemented) in the same namespace
as its declartation.

/Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,588
Members
45,093
Latest member
Vinaykumarnevatia00

Latest Threads

Top