473,624 Members | 2,026 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::cons t_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::myclas s () :
itsStruct()
{
itsStruct.itsNu mber = 0;
}

ostream& operator<< (ostream& dest, const myclass::mc_str uct& 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::TextBlo ck'
// (or there is no acceptable conversion)

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

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

} // namespace namsp2
} // namespace namsp1

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

TIA
Oct 27 '05 #1
6 1651
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::cons t_iterator i, iend;

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

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

TextBlock::cons t_iterator iend = tb.end();
for (TextBlock::con st_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::myclas s () :
itsStruct()
{
itsStruct.itsNu mber = 0;
}

ostream& operator<< (ostream& dest, const myclass::mc_str uct& 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::TextBlo ck'
// (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::operato r<< (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::cons t_iterator i, iend;

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

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


Don't do that:

TextBlock::cons t_iterator iend = tb.end();
for (TextBlock::con st_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_str uct& 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::TextBlo ck'
// (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::cons t_iterator i, iend;

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

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


Don't do that:

TextBlock::cons t_iterator iend = tb.end();
for (TextBlock::con st_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
29617
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 were still an option. However, I am coming up dry looking for information on how to do this, and the most recent references I am able to find to nested classes in PHP are dated 2003. And they all say the same thing: sorry, can't do that.
4
1533
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 follows: namespace MyApplication { namespace GUI {
14
3323
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
2763
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 in a nested scope, on variables from the outer scope: PythonWin 2.4.3 (#69, Mar 29 2006, 17:35:34) on win32. Portions Copyright 1994-2004 Mark Hammond (mhammond@skippinet.com.au) -
78
4935
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 problem is that my little helper function doesn't work! It claims that a variable doesn't exist. If I move the variable declaration, it finds the variable, but can't change it. Declaring the variable global in the nested function doesn't work...
3
1912
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 outside the namespace at the location where the namespace definition is located." I have some doubt about the using directives for the nested namespace, so I wrote a program to test it, #include <iostream>
10
4621
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: Accept the width of the largest square Output: A series of nested squares with 1 space between them Sample: > eca4.exe 10 ********** * * * ****** *
1
5257
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
8238
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8174
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8680
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8336
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8478
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5565
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2607
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1786
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1485
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.