D
Douglas
**** 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\stacktest.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:stream;
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<<(ostream & output, Stack<T> &a) //output type was
ostream&
{
cout << "\n\nElements 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
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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\stacktest.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:stream;
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<<(ostream & output, Stack<T> &a) //output type was
ostream&
{
cout << "\n\nElements 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
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=