|
-
May 8th, 2008, 05:45 AM
#1
namespace confusion
Hi everyone,
I´ve got some namespace problems I don´t really understand. I´ve got a header and an implementation file where three functions reside.
test.h
Code:
#ifndef TestH
#define TestH
namespace A {
namespace B {
namespace C {
int get_x();
int get_y();
int get_z();
};
};
};
#endif
test.cpp, working version
Code:
#include "test.h"
int A::B::C::get_x()
{
return 0;
}
int A::B::C::get_y()
{
return get_x() +1;
}
int A::B::C::get_z()
{
return get_y() +2;
}
test.cpp version 2, not working.
Code:
#include "test.h"
using namespace A::B::C;
int get_x()
{
return 0;
}
int get_y()
{
return get_x() +1;
}
int get_z()
{
return get_y() +2;
}
Here´s the calling code
Code:
#include "test.h"
int main()
{
int x = A::B::C::get_x();
int y = A::B::C::get_y();
int z = A::B::C::get_y();
}
My compiler (Borland C++ Builder 6) yields two error messages:
(1) ambiguity between A::B::C::get_x() and get_x()
(2) ambiguity between A::B::C::get_y() and get_y()
Now I have a couple of questions:
1) There are no functions get_x() and get_y() in the global namespace, so why is there an ambiguity?
2) Do I have to fully qualify the function name in my implementation file or does something exists that saves me from the extra typing?
Last edited by GNiewerth; May 8th, 2008 at 05:52 AM.
- Guido
-
May 8th, 2008, 05:54 AM
#2
Re: namespace confusion
1) There are no functions get_x() and get_y() in the global namespace,
Yes there are.
Code:
int get_x()
{
return 0;
}
int get_y()
{
return get_x() +1;
}
int get_z()
{
return get_y() +2;
}
Do I have to fully qualify the function name in my implementation file
Either that or do the following...
Code:
namespace A {
namespace B {
namespace C {
int get_x()
{
return 0;
}
int get_y()
{
return get_x() +1;
}
int get_z()
{
return get_y() +2;
}
}
}
}
-
May 8th, 2008, 05:57 AM
#3
Re: namespace confusion
Very useful, thank you. For some reasons I was convinced that the using directive tells the compiler where to put the function code, but it seems I was wrong.
- Guido
-
May 8th, 2008, 06:05 AM
#4
Re: namespace confusion
 Originally Posted by GNiewerth
Very useful, thank you. For some reasons I was convinced that the using directive tells the compiler where to put the function code, but it seems I was wrong.
What if you had more than one using directive at the beginning of your code? ;-)
More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf
Premature optimization is the root of all evil --Donald E. Knuth
Please read Information on posting before posting, especially the info on using [code] tags.
-
May 8th, 2008, 06:37 AM
#5
Re: namespace confusion
Think of it this way: it's a using directive, not a defining directive - it works when you want to use a member, but not when you want to define a member.
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
May 8th, 2008, 10:01 AM
#6
Re: namespace confusion
You can do something like this though:
Code:
//test.h
namespace A
{
namespace B
{
namespace C
{
void f();
}
}
}
//test.cpp
namespace ABC = A::B::C;
void ABC::f(){}
to save the typing. Hope it works.
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|