 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
SpOiLeR Guest
|
Posted: Thu Oct 27, 2005 10:47 am Post subject: 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
|
|
| Back to top |
|
 |
eiji Guest
|
Posted: Thu Oct 27, 2005 12:06 pm Post subject: Re: Nested namespace problem. Please help. |
|
|
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
|
|
| Back to top |
|
 |
SpOiLeR Guest
|
Posted: Thu Oct 27, 2005 1:22 pm Post subject: Re: Nested namespace problem. Please help. |
|
|
On 27 Oct 2005 05:06:30 -0700, eiji wrote:
| Quote: | using namespace XXX
should be inside the required namespace.
Try this:
#include <iostream
#include
#include
namespace namsp1 {
using namespace std;
typedef list
....
}
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.
|
|
| Back to top |
|
 |
Jonathan Mcdougall Guest
|
Posted: Thu Oct 27, 2005 1:28 pm Post subject: Re: Nested namespace problem. Please help. |
|
|
SpOiLeR wrote:
| Quote: | Why doesn't following code compile? Problem line is commented in code.
|
Don't use tabs. Use spaces.
| Quote: | ---------------------- Cut here-------------------------------
#include <iostream
#include
#include
using namespace std;
namespace namsp1 {
typedef list
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;
}
| Quote: | }
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;
};
| Quote: |
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.
| Quote: |
// 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
|
|
| Back to top |
|
 |
SpOiLeR Guest
|
Posted: Fri Oct 28, 2005 6:35 am Post subject: Re: Nested namespace problem. Please help. |
|
|
On 27 Oct 2005 06:28:38 -0700, Jonathan Mcdougall wrote:
| Quote: | 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...
| Quote: |
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)?
| Quote: |
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.
|
|
| Back to top |
|
 |
Jonathan Mcdougall Guest
|
Posted: Fri Oct 28, 2005 11:47 am Post subject: Re: Nested namespace problem. Please help. |
|
|
SpOiLeR wrote:
| Quote: | 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-lite/ctors.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-lite/operator-overloading.html#faq-13.15)
Have a look at the faq while you are there.
Jonathan
|
|
| Back to top |
|
 |
SpOiLeR Guest
|
Posted: Fri Oct 28, 2005 6:02 pm Post subject: Re: Nested namespace problem. Please help. |
|
|
On 28 Oct 2005 04:47:24 -0700, Jonathan Mcdougall wrote:
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...
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|