Nested namespace problem. Please help.

S

SpOiLeR

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::eek:stream& operator<< (std::eek:stream& 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::eek:perator<< (dest, mc.itsText);

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

} // namespace namsp2
} // namespace namsp1

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

TIA
 
E

eiji

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
 
S

SpOiLeR

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.
 
J

Jonathan Mcdougall

SpOiLeR said:
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::eek:stream& operator<< (std::eek:stream& 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::eek:perator<< (dest, mc.itsText);

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


Jonathan
 
S

SpOiLeR

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...
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)?
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.
 
J

Jonathan Mcdougall

S

SpOiLeR


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...
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top