namespace problem

V

vsgdp

I have a namespace with a collection of related functions. In the cpp file
for their implementations I have:

#include ...

using namespace n;

float f(){...}

void g(...)
{
f();
}

I get an error saying that 'f' is an ambiguous call to an overloaded
function...

If I change my cpp to the following, it works, :

#include ...

float n::f(){...}

void n::g(...)
{
f();
}

Why does the latter work and not the former? Also, in the latter
implementation of g, why don't I have to write

void n::g(...)
{
n::f();
}

Thanks.
 
V

Victor Bazarov

vsgdp said:
I have a namespace with a collection of related functions. In the cpp file
for their implementations I have:

#include ...

using namespace n;

float f(){...}

void g(...)
{
f();
}

I get an error saying that 'f' is an ambiguous call to an overloaded
function...

Do you have another 'f' somewhere, in the global scope? Ambiguity
can only exist if there is more than one to choose from.
If I change my cpp to the following, it works, :

#include ...

float n::f(){...}

void n::g(...)
{
f();
}

Why does the latter work and not the former? Also, in the latter
implementation of g, why don't I have to write

void n::g(...)
{
n::f();

Once you're in a function 'g' (who's part of 'n' namespace), you don't
have to specify it, 'n' is searched first, and 'n::f' is found.

Victor
 
J

John Carson

vsgdp said:
I have a namespace with a collection of related functions. In the
cpp file for their implementations I have:

#include ...

using namespace n;

float f(){...}

void g(...)
{
f();
}

I get an error saying that 'f' is an ambiguous call to an overloaded
function...

If I change my cpp to the following, it works, :

#include ...

float n::f(){...}

void n::g(...)
{
f();
}

Why does the latter work and not the former? Also, in the latter
implementation of g, why don't I have to write

void n::g(...)
{
n::f();
}

Thanks.


I suggest you do it this way:

namespace n
{
float f(){...}

void g(...)
{
f();
}
}

using namespace n;

dumps all of the content from the n namespace into the global namespace. By
contrast

namespace n
{
// stuff
}

re-opens namespace n and allows you to make additions to it.
 
K

Kevin Goodsell

Victor said:
Do you have another 'f' somewhere, in the global scope? Ambiguity
can only exist if there is more than one to choose from.

Isn't the 'f' above actually in the global scope? I figured the
ambiguity was between the 'f' above :):f) and the 'f' that is declared
in the header file as n::f, but for which no definition exists.

-Kevin
 
R

Rolf Magnus

Victor said:
Do you have another 'f' somewhere, in the global scope?

I guess not, but he's defining one in his example. And since he put a
"using namespace n;" at the top, the f declared in the header is also
put in the global namespace.
Ambiguity can only exist if there is more than one to choose from.

There are two to choose from.
 
A

Agent Mulder

vsgdp> I have a namespace...

Maybe this little program gives you the clue:


class Furry{};//comment this out and it compiles
namespace Green
{
class Furry{};//OR comment this out and it compiles
}
using namespace Green;//OR comment this out and it compiles
int main(int argc,char**argv)
{
Furry furry;//OR comment this out and it compiles
return 0;
}

-X
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,054
Latest member
LucyCarper

Latest Threads

Top