473,574 Members | 3,142 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

does not compile with "using namespace std"

**** Post for FREE via your newsreader at post.usenet.com ****

Hello,

The following code does not compile if line 3 is uncommented "using
namespace std".
I do not understand it. Could somebody explain it to me? I am using MSVC
6.0.
Thanks a lot,

Douglas

Error message is:

C:\test\stackte st.cpp(36) : error C2248: 'sp' : cannot access private member
declared in class 'Stack<char>'
// --------------- CODE STARTS HERE --------------------------
#include <iostream>

#define STACK_SIZE 10

// using namespace std; // It does not compile unless this line is
commented.

using std::ostream;
using std::cin;
using std::cout;

template<class T> class Stack {

public:
Stack() {items = new T[STACK_SIZE]; sp = -1;}
~Stack() {delete [ ] items;}
T top() const { return items[sp];}
T pop() {return items[sp--];}
void push(T i) {items[++sp] = i;}

friend ostream& operator<< (ostream &, Stack<T>&); // return type was
ostream&

private:
T* items;
int sp;
st
};
template<class T>
ostream& operator<<(ostr eam & output, Stack<T> &a) //output type was
ostream&
{
cout << "\n\nElemen ts in stack are: ";
for (int i=0; i< STACK_SIZE; i++)
output << a.pop();
output << a.sp;
return output;
}

int main()
{
Stack<char> a;
char ans;
cout << "Enter elements in stack: ";
for (int i =0; i< STACK_SIZE; i++)
{
std::cin >> ans;
a.push(ans);
}

cout << a;

return 0;
}


-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
*** Usenet.com - The #1 Usenet Newsgroup Service on The Planet! ***
http://www.usenet.com
Unlimited Download - 19 Seperate Servers - 90,000 groups - Uncensored
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Jul 22 '05 #1
8 4364
Douglas wrote:
I do not understand it. Could somebody explain it to me? I am using MSVC
6.0. // using namespace std; // It does not compile unless this line is
commented.
VC++ has bugs that help you write better code.
using std::ostream;
using std::cin;
using std::cout;


Only import identifiers with these, or typedefs. When you dump an entire
namespace into your module, you defeat the purpose of namespaces.

--
Phlip
http://industrialxp.org/community/bi...UserInterfaces
Jul 22 '05 #2
but I think such a product like VC++ won't have such bugs like that.......
"Phlip" wrote
Douglas wrote:
I do not understand it. Could somebody explain it to me? I am using MSVC
6.0.

// using namespace std; // It does not compile unless this line is
commented.


VC++ has bugs that help you write better code.
using std::ostream;
using std::cin;
using std::cout;


Only import identifiers with these, or typedefs. When you dump an entire
namespace into your module, you defeat the purpose of namespaces.

--
Phlip
http://industrialxp.org/community/bi...UserInterfaces

Jul 22 '05 #3
On Tue, 20 Jul 2004 12:52:48 +0800, Stone Lan <st*********@ho tmail.com>
wrote:
but I think such a product like VC++ won't have such bugs like
that.......


Your faith is very touching, but totally misplaced.

john
Jul 22 '05 #4
John Harrison wrote:
Stone Lan wrote:
but I think such a product like VC++ won't have such bugs like
that.......


Your faith is very touching, but totally misplaced.


Partly misplaced.

The top-poster responded to my "VC++ has bugs that help you write better
code."

Of course that's a joke. However, in some small sick way, if enough
programmers try to do X, and can't, but should, MS might respond. So market
forces eventually drill a hole thru the bugs, permitting the most beneficial
programmer activities thru.

I have seen very large programs with absurdly twisted that constantly named
namespaces, then threw the spaces away by using "using namespace" to insert
everything into everything else. If you changed one of them, VC++ would stop
compiling.

--
Phlip
http://industrialxp.org/community/bi...UserInterfaces
Jul 22 '05 #5
2004-7-20 14:41:39

Phlip <ph*******@yaho o.com> wrote in message

<D8************ ***@newssvr16.n ews.prodigy.com >
John Harrison wrote:
Stone Lan wrote:
but I think such a product like VC++ won't have such bugs like that.......

Your faith is very touching, but totally misplaced.

Partly misplaced. The top-poster responded to my "VC++ has bugs that help you write better
code." Of course that's a joke. However, in some small sick way, if enough programmers try to do X, and can't, but should, MS might respond. So market
forces eventually drill a hole thru the bugs, permitting the most beneficial
programmer activities thru. I have seen very large programs with absurdly twisted that constantly named
namespaces, then threw the spaces away by using "using namespace" to insert
everything into everything else. If you changed one of them, VC++ would stop
compiling. -- Phlip http://industrialxp.

org/community/bin/view/Main/TestFirstUserIn terfaces

In fact I am always using "using namespace std" in VC++6.0,but I
didn't have the problem.Why?
--
Composed with Newz Crawler 1.7 http://www.newzcrawler.com/
Jul 22 '05 #6
Douglas wrote:

**** Post for FREE via your newsreader at post.usenet.com ****

Hello,

The following code does not compile if line 3 is uncommented "using
namespace std".
I do not understand it. Could somebody explain it to me? I am using MSVC
6.0.
Thanks a lot,

Douglas

While I cannot give you any insight on the way MSVC 6.0 works, your
code does have a problem that needs fixing, one way or the other,
or else it will not work on more conforming compilers.
See comments below (only on that specific problem, ignoring other
issues).

Error message is:

C:\test\stackte st.cpp(36) : error C2248: 'sp' : cannot access private member
declared in class 'Stack<char>'

// --------------- CODE STARTS HERE --------------------------
#include <iostream>

#define STACK_SIZE 10

// using namespace std; // It does not compile unless this line is
commented.

using std::ostream;
using std::cin;
using std::cout;

template<class T> class Stack {

public:
Stack() {items = new T[STACK_SIZE]; sp = -1;}
~Stack() {delete [ ] items;}
T top() const { return items[sp];}
T pop() {return items[sp--];}
void push(T i) {items[++sp] = i;}

friend ostream& operator<< (ostream &, Stack<T>&); // return type was
ostream&
The above is a declaration of a normal function, not a function
template. That means that a specialisation of class Stack for some
template parameter T will have the following function
ostream& operator <<(ostream&, Stack<T>&);
as a friend. Again, the above is a normal function, not a
specialisation of a function template.


private:
T* items;
int sp;
st ^^
Just a typo, I guess.

};

template<class T>
ostream& operator<<(ostr eam & output, Stack<T> &a) //output type was
ostream&
{
cout << "\n\nElemen ts in stack are: ";
for (int i=0; i< STACK_SIZE; i++)
output << a.pop();
output << a.sp;
return output;
}


Now this is a function template, which is unrelated to the declaration
friend ostream& operator <<(ostream&, Stack<T>&);
above.

Your code should compile with the right compiler, but it won't link
because you haven't provided a definition for the friend operator <<.

There are several ways you can implement what you want, but I'm not
sure which one of them (if any) VC 6.0 will accept (I'm not bashing
MSVC, I just cannot take it for a test run).

0. Define what you have declared, a properly typed operator << for
your Stack specialisation:
ostream& operator <<(ostream& output, Stack<char>& a) {...}

It's a kludge. I would avoid using it if at all possible.

1. Declare your friend as
friend ostream& operator<< <>(ostream &, Stack<T>&);

in which case you will also need a couple of forward declarations
just before the line "template<c lass T> class Stack {":

template<class T> class Stack;
template<class T>
ostream& operator <<(ostream& output, Stack<T>& a);

The effect is that a specialisation of Stack will have a
corresponding specialisation of the /function template/ operator <<
as a friend. Leave the definition of operator << as it is.

2. You can let the Stack declare a friend template:
template <class T1>
friend ostream& operator <<(ostream &, Stack<T1>&);
Now all specialisations of this operator << will be friends of
any specialisation of Stack. Though this probably won't be a
big problem, it is likely not what you really want either.

#1 should be the default choice to consider.

All of the above should work. If everything fails, you can at least
use a work-around: define a public member function for the Stack class
template:

ostream& print(ostream& s) {...}

that does all of the job the operator << is supposed to do. Then
make operator << template call this function.

(I think all of the above has been discussed here before, I've
just restored it by memory).

Denis
Jul 22 '05 #7
On Tue, 20 Jul 2004 06:41:39 GMT, Phlip <ph*******@yaho o.com> wrote:
John Harrison wrote:
Stone Lan wrote:
> but I think such a product like VC++ won't have such bugs like
> that.......


Your faith is very touching, but totally misplaced.


Partly misplaced.

The top-poster responded to my "VC++ has bugs that help you write better
code."

Of course that's a joke. However, in some small sick way, if enough
programmers try to do X, and can't, but should, MS might respond. So
market
forces eventually drill a hole thru the bugs, permitting the most
beneficial
programmer activities thru.

I have seen very large programs with absurdly twisted that constantly
named
namespaces, then threw the spaces away by using "using namespace" to
insert
everything into everything else. If you changed one of them, VC++ would
stop
compiling.


This is a true story.

When Windows 95 was released MS were obviously very keen that as many DOS
programs as possible would run without problems. One that didn't was Sim
City. MS were pretty sure that the problem was in Sim City so they decided
that they would debug Sim City themselves. Turned out the problem was in
heap use. Sim City was freeing memory and for a short time continuing to
use that memory. In a single tasking system like DOS they got away with
it, but in a multitasking system some other process would grab the memory
that Sim City was trying to use.

What was MS's next action? Did they alert the Sim City programmers and say
'Hey you've got a bug in Sim City, you better fix it'. Not at all. What
they did was to write a special heap allocation algorithm especially for
Sim City. This algorithm said 'if you are running Sim City work the heap
this way, for everyone else work the heap the usual way'. Thus working
around the problem.

My slightly round about point is that I don't think market forces benefit
coding standards, common programming practises are not necessarily good
programming practises. And market forces will always go for the short term
benefit at possible long term cost.

I think market forces alone would dictate 'if enough programmers try to do
X, even though they shouldn't, MS would respond'.

john
Jul 22 '05 #8
Stone Lan wrote:
In fact I am always using "using namespace std" in VC++6.0,but I
didn't have the problem.Why?


There are two problems.

In a small program, you should not use that statement, because you want your
program to remain small, and easy to understand. Use the fewest symbols
possible, not all of them, and make the lists of what you import explicit.

using std::string;
using std::cout; // etc

In a big program, VC++ will puke its internal symbol tables, due to bugs
caused spending too much effort reacting to sloppy programmers' other bad
habits, and not enough effort on raw Standards compliance.

--
Phlip
http://industrialxp.org/community/bi...UserInterfaces
Jul 22 '05 #9

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

Similar topics

0
1792
by: Justin | last post by:
Hi. I decided to tidy up some of my queries, but I came across something that stumpt me: mysql> SELECT -> jobs.jobId, -> jobs.active, -> jobs.title, -> jobs.listed, -> industries.industry, -> occupations.occupation,
5
1941
by: cppaddict | last post by:
It is typical to put the line: using namespace std; at the top of a file which makes use of std library objects. To take a simple example: #include <iostream> using namespace std;
2
6385
by: Jacek Dziedzic | last post by:
Is it valid to use a "using namespace foo" (as opposed to using foo::bar which I'm sure is legal) within a class declaration? My compiler rejects it, but I've been told it's valid. Can anyone please confirm or deny? TIA, - J.
4
2696
by: Teddy | last post by:
If I use just a STL container such as std::vector in my program. Is "using std::vector;" better than "using namespace std" ? Does "using namespace std" cost ?
13
141224
by: Squid Seven | last post by:
This is just bizarre. for the following snippet of code: #include <string> using std::string; I get the error message:
6
4508
by: AlexD_UK | last post by:
When I create a new C++ project of type "Class Library (.NET)", I am unable to then add the following line of code : using namespace std If I do, I get the following error on compilation : c:\Research\Code\Visual Studio\TestC++2\TestC++2.h(7): error C2871: 'std' : a namespace with this name does not exist If I try this with other project...
30
4084
by: Pep | last post by:
Is it best to include the code "using namespace std;" in the source or should each keyword in the std namespace be qualified by the namespace tag, such as std::cout << "using std namespace" << std::endl; Myself I am not sure which I prefer, it is certainly easier to specify that the std namespace is being used instead of tagging each...
12
2394
by: Steve Pope | last post by:
Compiling the following works on my system: file main.cpp: #include <iostream> namespace space { int foo; }
25
2558
by: samjnaa | last post by:
Please check for sanity and approve for posting at python-dev. In Visual Basic there is the keyword "with" which allows an object- name to be declared as governing the following statements. For example: with quitCommandButton .enabled = true .default = true end with
0
7841
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...
0
8100
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. ...
0
8270
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8137
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...
0
6509
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5335
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...
0
3793
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2270
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
1369
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.