473,387 Members | 1,590 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Simple question (namespaces and operators)

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

Aug 9 '06 #1
3 1489
Marco T. wrote:
//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.
A method() {A a1,a2; return a1*a2;} //line X
Regards,
Sumit.
Aug 9 '06 #2

Sumit RAJAN wrote:
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!

:-)

Aug 9 '06 #3

Marco T. wrote:
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 ::operator *(A &a, A &b)
or ciao::operator *(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

Aug 9 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Igorati | last post by:
Hello all, I am still needing some help on this code, I have gone a bit further on it. Thank you for the help. I am trying to understand how to make the file searchable and how I am to make the...
2
by: glen stark | last post by:
Hi. I had an interesting experience with a brand x compiler recently. I had class defined within a namespace, and in that class I declared the "<<" operator as a friend to that class. Then, in...
4
by: GianGuz | last post by:
Global new and delete operators can be overloaded to suite particulars needs. Typically they are overloaded to insert useful debugging/trace informations. What I would to discuss here concerns the...
4
by: George Economos | last post by:
Hi all, I am using msvc 7.1 and have encountered the following code: ----------- A.hpp ----------- class A {
4
by: cody | last post by:
I have the following feed which I want to convert to html: http://www.heise.de/newsticker/heise.rdf but it does not work, I only see the html elements empty as if it found no elements in the xml...
10
by: Douglas G | last post by:
I've tried various ideas on this problem, but I don't see word wrapping. Can you point out what is wrong? It's a K&R exercise, and I'm still new to programming. Other pointers would be helpful...
4
by: Ranginald | last post by:
Sorry for the simple question but thanks in advance: My goal is to create reusale code for a web app in C#. I have written the code already as a windows app but here is where I am confused: ...
1
by: Alexander Bürger | last post by:
Hi, I have a question about operators. I have something similar to the following code: //================ // file a.h class A { ... }; // file op.h
3
by: Alexander Bürger | last post by:
Hi, I have a little problem with classes in a namespace and an operator defined for these classes outside that namespace. Example: //...
11
by: jakester | last post by:
I am using Visual C++ 2007 to build the code below. I keep getting linkage error. Could someone please tell me what I am doing wrong? The code works until I start using namespace for my objects. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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,...
0
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...

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.