By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,369 Members | 1,538 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,369 IT Pros & Developers. It's quick & easy.

Nested namespace problem. Please help.

P: n/a
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
Share this Question
Share on Google+
6 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.