Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > namespace problem

Reply
Thread Tools

namespace problem

 
 
vsgdp
Guest
Posts: n/a
 
      08-20-2003
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.


 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      08-20-2003
"vsgdp" <(E-Mail Removed)> wrote...
> 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


 
Reply With Quote
 
 
 
 
John Carson
Guest
Posts: n/a
 
      08-20-2003
"vsgdp" <(E-Mail Removed)> wrote in message
news:OhB0b.5718$Qy4.2192@fed1read05
> 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.


--

John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)


 
Reply With Quote
 
Kevin Goodsell
Guest
Posts: n/a
 
      08-20-2003
Victor Bazarov wrote:
> "vsgdp" <(E-Mail Removed)> wrote...
>
>>
>>#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.


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
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      08-20-2003
Victor Bazarov wrote:

> "vsgdp" <(E-Mail Removed)> wrote...
>> 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?


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.

 
Reply With Quote
 
Agent Mulder
Guest
Posts: n/a
 
      08-20-2003
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










 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ERROR CS0234: The type or namespace name 'DataAccessHelper' does not exist in the namespace 'BCC' (are you missing an assembly reference?) li.eddie@gmail.com ASP .Net 0 01-06-2006 11:31 AM
[XML Schema] Including a schema document with absent target namespace to a schema with specified target namespace Stanimir Stamenkov XML 3 04-25-2005 09:59 AM
Reaching into the default namespace when using another namespace. Jason Heyes C++ 1 11-19-2004 02:36 AM
Namespace: Is it a scope or a namespace? Anonymous C++ 3 08-18-2003 01:31 PM
Help:Why can't I use namespace System.Web? It is said that this namespace doesn't exist. But it should exist. ASP .Net 1 07-29-2003 04:31 PM



Advertisments