473,498 Members | 1,907 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Nested namespace problem. Please help.

Why doesn't following code compile? Problem line is commented in code.

---------------------- Cut here-------------------------------

#include <iostream>
#include <list>
#include <string>

using namespace std;

namespace namsp1 {

typedef list<string> TextBlock;

ostream& operator<< (ostream& dest, const TextBlock& tb)
{
if (dest) {
TextBlock::const_iterator i, iend;

i = tb.begin();
iend = tb.end();

for (; i != iend; i++)
dest << *i << endl;
}

return dest;
}

namespace namsp2 {

class myclass
{
public:
myclass ();

typedef struct {
TextBlock itsText;
int itsNumber;
} mc_struct;

friend std::ostream& operator<< (std::ostream& dest, const mc_struct&
mc);

private:
mc_struct itsStruct;
};

myclass::myclass () :
itsStruct()
{
itsStruct.itsNumber = 0;
}

ostream& operator<< (ostream& dest, const myclass::mc_struct& mc)
{
if (dest) {
// Next one doesn't work
dest << mc.itsText;
// error returned is:
// binary '<<' : no operator found which takes a right-hand operand
// of type 'const namsp1::TextBlock'
// (or there is no acceptable conversion)

// Next line solves the problem but why the previous doesn't work?
// namsp1::operator<< (dest, mc.itsText);

dest << mc.itsNumber;
}
return dest;
}

} // namespace namsp2
} // namespace namsp1

---------------------- Cut here-------------------------------

TIA
Oct 27 '05 #1
6 1631
using namespace XXX
should be inside the required namespace.

Try this:

#include <iostream>
#include <list>
#include <string>

namespace namsp1 {

using namespace std;

typedef list<string> TextBlock;
.....

}

I hope that works.
Regards

Oct 27 '05 #2
SpOiLeR wrote:
Why doesn't following code compile? Problem line is commented in code.
Don't use tabs. Use spaces.
---------------------- Cut here-------------------------------

#include <iostream>
#include <list>
#include <string>

using namespace std;

namespace namsp1 {

typedef list<string> TextBlock;

ostream& operator<< (ostream& dest, const TextBlock& tb)
{
if (dest) {
TextBlock::const_iterator i, iend;

i = tb.begin();
iend = tb.end();

for (; i != iend; i++)
dest << *i << endl;
Don't do that:

TextBlock::const_iterator iend = tb.end();
for (TextBlock::const_iterator i=tb.begin(); i!=iend; ++i)
{
dest << *i << endl;
}
}

return dest;
}

namespace namsp2 {

class myclass
{
public:
myclass ();

typedef struct {
TextBlock itsText;
int itsNumber;
} mc_struct;
You don't need that in C++:

struct mc_struct
{
TextBlock itsText;
int itsNumber;
};

friend std::ostream& operator<< (std::ostream& dest,
const mc_struct& mc);

private:
mc_struct itsStruct;
};

myclass::myclass () :
itsStruct()
{
itsStruct.itsNumber = 0;
}

ostream& operator<< (ostream& dest, const myclass::mc_struct& mc)
{
if (dest) {
// Next one doesn't work
dest << mc.itsText;
// error returned is:
// binary '<<' : no operator found which takes a right-hand operand
// of type 'const namsp1::TextBlock'
// (or there is no acceptable conversion)
TextBlock is a typedef. Therefore, Koenig lookup applies to the
underlying type, namely std::list. It does not look in namsp1. If you
make TextBlock a class instead of a typedef, its namespace will be
searched correctly.

If you wonder why, imagine what could the compiler do instead. Search
namespace namsp1? And if it does not find any operator there, search
namespace std? What if the typedef is 4 layers deep in different
namespaces? Search them all?

Koenig lookup only applies to the "real" type.

// Next line solves the problem but why the previous doesn't work?
// namsp1::operator<< (dest, mc.itsText);


Of course, that is a qualified call, it cannot fail (if that operator<<
exists, of course).
Jonathan

Oct 27 '05 #3
On 27 Oct 2005 05:06:30 -0700, eiji wrote:
using namespace XXX
should be inside the required namespace.

Try this:

#include <iostream>
#include <list>
#include <string>

namespace namsp1 {

using namespace std;

typedef list<string> TextBlock;
....

}

I hope that works.
Regards


Nope, it doesn't... Anyway it is not the problem with finding something in
namespace std (in my or your way). Problem is that method from namespace
namsp2 which is nested in namsp1, can't find a method from namsp1, and I
was wondering why is that.
Oct 27 '05 #4
On 27 Oct 2005 06:28:38 -0700, Jonathan Mcdougall wrote:
SpOiLeR wrote:
Why doesn't following code compile? Problem line is commented in code.


Don't use tabs. Use spaces.


copy-paste from my IDE, I know why it's bad but forgot to set it up after
fresh install few weeks ago, sorry...

ostream& operator<< (ostream& dest, const TextBlock& tb)
{
if (dest) {
TextBlock::const_iterator i, iend;

i = tb.begin();
iend = tb.end();

for (; i != iend; i++)
dest << *i << endl;


Don't do that:

TextBlock::const_iterator iend = tb.end();
for (TextBlock::const_iterator i=tb.begin(); i!=iend; ++i)
{
dest << *i << endl;
}


What's the difference (except scope of i being changed)?

ostream& operator<< (ostream& dest, const myclass::mc_struct& mc)
{
if (dest) {
// Next one doesn't work
dest << mc.itsText;
// error returned is:
// binary '<<' : no operator found which takes a right-hand operand
// of type 'const namsp1::TextBlock'
// (or there is no acceptable conversion)


TextBlock is a typedef. Therefore, Koenig lookup applies to the
underlying type, namely std::list. It does not look in namsp1. If you
make TextBlock a class instead of a typedef, its namespace will be
searched correctly.

If you wonder why, imagine what could the compiler do instead. Search
namespace namsp1? And if it does not find any operator there, search
namespace std? What if the typedef is 4 layers deep in different
namespaces? Search them all?

Koenig lookup only applies to the "real" type.


Thanks, I didn't know all this. You've been very helpfull.
Oct 28 '05 #5
SpOiLeR wrote:
On 27 Oct 2005 06:28:38 -0700, Jonathan Mcdougall wrote:
ostream& operator<< (ostream& dest, const TextBlock& tb)
{
if (dest) {
TextBlock::const_iterator i, iend;

i = tb.begin();
iend = tb.end();

for (; i != iend; i++)
dest << *i << endl;


Don't do that:

TextBlock::const_iterator iend = tb.end();
for (TextBlock::const_iterator i=tb.begin(); i!=iend; ++i)
{
dest << *i << endl;
}


What's the difference (except scope of i being changed)?


1) initialization instead of assignment

int a; a=1; // assignement
int a=1; // initialization

(http://www.parashift.com/c++-faq-lit....html#faq-10.6)

2) scope of i (as you said)

3) prefix form of operator++ instead of postfix

++i; // prefix
i++; // postfix

(http://www.parashift.com/c++-faq-lit...html#faq-13.15)

Have a look at the faq while you are there.
Jonathan

Oct 28 '05 #6
On 28 Oct 2005 04:47:24 -0700, Jonathan Mcdougall wrote:

(http://www.parashift.com/c++-faq-lit....html#faq-10.6)

...

(http://www.parashift.com/c++-faq-lit...html#faq-13.15)

Have a look at the faq while you are there.

Jonathan


Well, about a year ago I started learning C++. I've read FAQ back then.
I've learnt a lot from that times, but I still can get surprised and
confused... And I can still learn new things by reading FAQ! I'll have a
moral of these posts put somewhere near my work desk, so I don't forget
it... Again, thanks for your help...
Oct 28 '05 #7

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

Similar topics

26
29591
by: Joshua Beall | last post by:
Hi All, I remember reading that both nested classes and namespaces would be available in PHP5. I know that namespaces got canceled (much sadness...), however, I *thought* that nested classes...
4
1524
by: rmacias | last post by:
I posted this in another forum, but nobody replied and I need an answer fairly quickly: I'm creating a Windows form application in VC++ .NET 2003. I'm creating the UI is a nested namespace as...
14
3299
by: Tiraman | last post by:
Hi , I would like to use nested namespace . I have 3 namespace as dll's : Namespace A Namespace B Namespace C And i want to have some namespace that contain them all , some thing like
37
2731
by: Tim N. van der Leeuw | last post by:
Hi, The following might be documented somewhere, but it hit me unexpectedly and I couldn't exactly find this in the manual either. Problem is, that I cannot use augmented assignment operators...
78
4878
by: Josiah Manson | last post by:
I found that I was repeating the same couple of lines over and over in a function and decided to split those lines into a nested function after copying one too many minor changes all over. The only...
3
1898
by: Wayne Shu | last post by:
When I read the chapter of the namespace of the book C++ Primer(3e). It explain the using directive as follow: "A using directive makes the namespace member names visible as if they were declared...
10
4603
by: wazzup | last post by:
C++ is new to me and I'm trying solve this problem but get stuck. Please help me out. Below is the task and after that is what i got so far. Create a program to print nested squares Input:...
1
5238
by: maverik | last post by:
Hi all. I have several nested namespaces: A::B::C::MyClass; Can I write in MyClass.h namespace A::B::C { class MyClass {
0
7167
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
7208
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...
1
6890
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7379
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5464
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4593
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3095
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3085
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
292
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.