Without "using namespace std;" How can the compiler locate namespace?

Please see the code in the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
================================================================================
////////////////////////////CircularList.h/////////////////////////////////////////////In this Header file, these is no "using namespace std", but I still can use cout, endl in the function definition. WHY?????????????????????////////////////////////////////////////////////////// 

template<typename Type> class CircularList;

template<typename Type> class ListNode{
private:
	friend class CircularList<Type>;
	ListNode():m_pnext(NULL){}
	ListNode(const Type item,ListNode<Type> *next=NULL):m_data(item),m_pnext(next){}
	~ListNode(){
		m_pnext=NULL;
	}
	
private:
	Type m_data;
	ListNode *m_pnext;
};

template<typename Type> class CircularList{
public:
	CircularList():head(new ListNode<Type>()){
		head->m_pnext=head;	
	}
	~CircularList(){
		MakeEmpty();
		delete head;
	}
public:
	void MakeEmpty();	//clear the list
	int Length();		//get the length
	ListNode<Type> *Find(Type value,int n);	//find the nth data which is equal to value
	ListNode<Type> *Find(int n);			 //find the nth data
	bool Insert(Type item,int n=0);	    //insert the data into the nth data of the list
	Type Remove(int n=0);					//delete the nth data
	bool RemoveAll(Type item);				//delete all the datas which are equal to value
	Type Get(int n);	//get the nth data
	void Print();		//print the list

private:
	ListNode<Type> *head;

};

template<typename Type> void CircularList<Type>::MakeEmpty(){
	ListNode<Type> *pdel,*pmove=head;
	while(pmove->m_pnext!=head){
		pdel=pmove->m_pnext;
		pmove->m_pnext=pdel->m_pnext;
		delete pdel;
	}
}

template<typename Type> int CircularList<Type>::Length(){
	ListNode<Type> *pmove=head;
	int count=0;
	while(pmove->m_pnext!=head){
		pmove=pmove->m_pnext;
		count++;
	}
	return count;
}

template<typename Type> ListNode<Type>* CircularList<Type>::Find(int n){
	if(n<0){
		cout<<"The n is out of boundary"<<endl;
		return NULL;
	}
	ListNode<Type> *pmove=head->m_pnext;
	for(int i=0;i<n&&pmove!=head;i++){
		pmove=pmove->m_pnext;
	}
	if(pmove==head){
		cout<<"The n is out of boundary"<<endl;
		return NULL;
	}
	return pmove;
}

template<typename Type> ListNode<Type>* CircularList<Type>::Find(Type value,int n){
	if(n<1){
		cout<<"The n is illegal"<<endl;
		return NULL;
	}
	ListNode<Type> *pmove=head;
	int count=0;
	while(count!=n){
		pmove=pmove->m_pnext;
		if(pmove->m_data==value){
			count++;
		}
		if(pmove==head){
			cout<<"can't find the element"<<endl;
			return NULL;
		}
	}
	return pmove;
}

template<typename Type> bool CircularList<Type>::Insert(Type item, int n){
	if(n<0){
		cout<<"The n is out of boundary"<<endl;
		return 0;
	}
	ListNode<Type> *pmove=head;
	ListNode<Type> *pnode=new ListNode<Type>(item);
	if(pnode==NULL){
		cout<<"Application error!"<<endl;
		exit(1);
	}
	for(int i=0;i<n;i++){
		pmove=pmove->m_pnext;
		if(pmove==head){
			cout<<"The n is out of boundary"<<endl;
			return 0;
		}
	}

	pnode->m_pnext=pmove->m_pnext;
	pmove->m_pnext=pnode;
	return 1;
}

template<typename Type> bool CircularList<Type>::RemoveAll(Type item){
	ListNode<Type> *pmove=head;
	ListNode<Type> *pdel=head->m_pnext;
	while(pdel!=head){
		if(pdel->m_data==item){
			pmove->m_pnext=pdel->m_pnext;
			delete pdel;
			pdel=pmove->m_pnext;
			continue;
		}
		pmove=pmove->m_pnext;
		pdel=pdel->m_pnext;
	}
	return 1;
}

template<typename Type> Type CircularList<Type>::Remove(int n){
	if(n<0){
		cout<<"can't find the element"<<endl;
		exit(1);
	}
	ListNode<Type> *pmove=head,*pdel;
	for(int i=0;i<n&&pmove->m_pnext!=head;i++){
		pmove=pmove->m_pnext;
	}
	if(pmove->m_pnext==head){
		cout<<"can't find the element"<<endl;
		exit(1);
	}
	pdel=pmove->m_pnext;
	pmove->m_pnext=pdel->m_pnext;
	Type temp=pdel->m_data;
	delete pdel;
	return temp;
}

template<typename Type> Type CircularList<Type>::Get(int n){
	if(n<0){
		cout<<"The n is out of boundary"<<endl;
		exit(1);
	}
	ListNode<Type> *pmove=head->m_pnext;
	for(int i=0;i<n;i++){
		pmove=pmove->m_pnext;
		if(pmove==head){
			cout<<"The n is out of boundary"<<endl;
			exit(1);
		}
	}
	return pmove->m_data;
}

template<typename Type> void CircularList<Type>::Print(){
	ListNode<Type> *pmove=head->m_pnext;
	cout<<"head";
	while(pmove!=head){
		cout<<"--->"<<pmove->m_data;
		pmove=pmove->m_pnext;
	}
	cout<<"--->over"<<endl<<endl<<endl;
}


================================================================================
///////////////////////////////Mian.cpp/////////////////////////////////////
#include <iostream>
#include "CircularList.h"

using namespace std;

int main()
{
	CircularList<int> list;
	for(int i=0;i<20;i++){
		list.Insert(i*3,i);
	}
	cout<<"the Length of the list is "<<list.Length()<<endl;
	list.Print();
	for(int i=0;i<5;i++){
		list.Insert(3,i*3);
	}
	cout<<"the Length of the list is "<<list.Length()<<endl;
	list.Print();

	list.Remove(5);
	cout<<"the Length of the list is "<<list.Length()<<endl;
	list.Print();

	list.RemoveAll(3);
	cout<<"the Length of the list is "<<list.Length()<<endl;
	list.Print();

	cout<<"The third element is "<<list.Get(3)<<endl;

	list.MakeEmpty();
	cout<<"the Length of the list is "<<list.Length()<<endl;
	list.Print();


	return 0;
}
The program can run correctly. I don't know why the compiler regarded namespace as std as the default.
This is the real code? You don't have using namespace std; before #include "CircularList.h" or something?


It's on line 192. The code prior to that doesn't seem to require it from my quick skim read.

@akluffy

Prefer to use the :: operator rather than using namespace, as in

std::cout

or do this :

using std::cout;

then cout without qualification.

HTH
I know that some implementations (such as MSVC++) contain global-namespace copies/aliases for some commonly-used members of std, so that you can simply use them without needing to qualify the namespace.

Could that be what's happening here?
Last edited on
He is using cout on line 66. The code does not compile for me and I don't understand why it would.
Peter,

You are right :-)

Good morning everyone!
And Peter,
Is the code not working in your IDE?

I am using VS2012, and it does work! So I am confused.
As I said, VS is one of those implementation that contains copies/aliases of some std stuff in the global namespace. That's probably what's going on.

EDIT: Wait a minute. Is that the whole of CircularList.h? Are you not including any header files in it? Because if it's using cout, it should include <iostream> in it.
Last edited on
Thank you MikeyBoy,

Maybe you are right!

But I write another program, and it doesnot work!!!Let me put code, please, ^_^~
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
=====================================================
//////////Header file///////
//Chain declaration
//Chain_ChianNode.h
#ifndef CHAIN_H
#define CHAIN_H

#include "stdafx.h"
#include <iostream>

template <typename T> class Chain;
template <typename T> class ChainIterator;

template <typename T>
class ChainNode {	
	T data;
	ChainNode<T> *link;
public:
	friend Chain<T>;
	friend ChainIterator<T>;
};

template <typename T>
class Chain {
	ChainNode<T> *first;
	ChainNode<T> *last;
public:
	Chain() {first = 0;} 
	~Chain();
	bool IsEmpty() const;
	int Length() const;
	bool Find(int k, T& x) const;
	int Search(const T& x) const;
	Chain<T>& Delete(int k, T& x);
	Chain<T>& Insert(int k, const T& x);
	void Output(std::ostream& out) const;
	void Erase();
	Chain<T>& Append(const T&x);
	
	friend ChainIterator<T>;
};

template <typename T>
class ChainIterator {
	ChainNode<T> *location;
public:
	T* Initialize(const Chain<T>& c)
	{
		location = c.first;
		if(location) return &location->data;
		return 0;
	}

	T* Next()
	{
		if(!location) return 0;
		location = location->link;
		if(location) return &location->data;
		return 0;
	}
};

template <typename T>	//delete all nodes
Chain<T>::~Chain()
{
	ChainNode<T> *next;
	while (first)
	{
		next = first->link;
		delete first;
		first = next;
	}
}

template <typename T>
bool Chain<T>::IsEmpty() const 
{
	return (first == 0);
}

template <typename T>	//Length of the Chain
int Chain<T>::Length() const
{
	ChainNode<T> *current = first;
	int len = 0;
	while(current)
	{
		len++;
		current = current->link;
	}
	return len;
}

template <typename T>
bool Chain<T>::Find(int k, T& x) const
{
	if(k < 1) return false;
	ChainNode<T> *current = first;
	int index = 1;	//current index
	while (index < k && current)
	{
		current = current->link;
		index++;
	}
	if(current) {x = current->data; return true;}
	return false;
}

template <typename T>
int Chain<T>::Search(const T& x) const
{
	ChainNode<T> *current = first;
	int index = 1;
	while (current && current->data != x)
	{
		current = current->link;
		index++;
	}
	if(current) return index;
	return 0;
}

template <typename T> //Output
void Chain<T>::Output(ostream& out) const
{
	ChainNode<T> *current;
	for(current = first; current; current = current->link)
	{
		out << current->data << " ";
	}
}

template <typename T>
ostream& operator<<(ostream& out, const Chain<T>& x)
{
	x.Output(out);
	return out;
}

template <typename T>	//delete one element, delete element k and put it to x
Chain<T>& Chain<T>::Delete(int k, T& x)
{
	if(k < 1 || !first)	//不存在第K个元素
		throw OutOfBounds();
	ChainNode<T> *p = first;
	if(k == 1)
		first = first->link;
	else {
		ChainNode<T> *q = first;
		for(int index = 1; index < k - 1 && q; index++) 
			q = q->link;	//q points to element k-1 address
		if(!q || !q->link)
			throw OutOfBounds();
		p = q->link;	// p points element k address
		if(p == last) last = q;
		q->link = p->link;	//q->link was pointing to p, and now points to p->link
		x = p->data;
		delete p;
	}
		return *this;	
}

template <typename T>	//Insert 
Chain<T>& Chain<T>::Insert(int k, const T& x)
{
	if(k < 0) throw OutOfBounds();

	ChainNode<T> *p = first;
	for(int index = 1; index < k && p; index++)
		p = p->link;	//move to element k
	if(k > 0 && !p) throw OutOfBounds();
	//Insert x after k
	ChainNode<T> *y = new ChainNode<T>;
	y->data = x;
	if(k) {
		y->link = p->link;
		p->link = y;
	}
	else {
		y->link = first;
		first = y;
	}
	if(!y->link) last = y;
	return *this;
}

template <typename T>
void Chain<T>::Erase()
{
	ChainNode<T> *next;
	while (first)
	{
		next = first->link;
		delete first;
		first = next;
	}
}

template <typename T> //add an element after the last element
Chain<T>& Chain<T>::Append(const T& x)
{
	ChainNode<T> *y;
	y = new ChainNode<T>;
	y->data = x;
	y->link = 0;
	if(first) {
		last->link = y;
		last = y;
	}
	else 
		first = last = y;
	return *this;
}
=====================================================
// PracticeProject.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"

////using namespace std//// If I put this namespace in this line, the program can work
#include "Chain_ChainNode.h"

using namespace std;


int main () 
{
	try {
		Chain<int> L;
		L.IsEmpty();
		cout << "Lenth = " << L.Length() << endl;
		L.Insert(0,100).Insert(1,101);
		cout << "List is : " << L << endl;
		L.Insert(1,102).Insert(1,103);
		cout << "List is : " << L << endl;
		int z;
		L.Find(2,z);
		cout << "the second element is : " << z << endl;
		L.Delete(1,z);
		cout << "The element has been deleted is : " << z << endl;
		cout << "List is : " << L << endl;
		L.Append(54321).Append(9876);	
		cout << "List is : " << L << endl;
		
		int *x;
		ChainIterator<int> c;
		x = c.Initialize(L);
		while(x) {
			cout << *x << ' ';
			x = c.Next();
		}
	}

	catch(...) {
		cerr << "An exception has occurred" << endl;
	}
}

Last edited on
using namespace std is your problem? Then I suggest that you forget it and add the five letter in front of the std functions. It's not that much...
Well, you're using a lot more symbols from std in Chain_ChianNode.h than you were in CircularList.h. If my original theory is correct, then I guess some of the additional symbols you're using here are ones that MS don't create copies of in the global namespace.

If you want it to work, the safest thing to do is to make sure all those symbols are prefaced with std:: in the header file.
MikeyBoy wrote:
As I said, VS is one of those implementation that contains copies/aliases of some std stuff in the global namespace. That's probably what's going on.


Those aliases in the global namespace only occur in the C library, so I don't think that's what is happening here. If one changes the code in main.cpp in the OP:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
///////////////////////////////Mian.cpp/////////////////////////////////////
#include <iostream>
#include "CircularList.h"

int main()
{
    CircularList<int> list;

    using namespace std;

    for(int i=0;i<20;i++){
        list.Insert(i*3,i);
    }
...


one gets the expected errors. It seems it uses whatever namespaces are visible at the point of instantiation, which makes an odd sort of sense, but probably isn't intentional and has the potential to introduce some puzzling issues if one isn't aware of it.

[edit: Apparently it is a bug that's been around awhile:
http://stackoverflow.com/questions/8226971/scope-resolution-for-template-instantiation ]
Last edited on
Those aliases in the global namespace only occur in the C library, so I don't think that's what is happening here. If one changes the code in main.cpp in the OP:

[...]

one gets the expected errors. It seems it uses whatever namespaces are visible at the point of instantiation, which makes an odd sort of sense, but probably isn't intentional and has the potential to introduce some puzzling issues if one isn't aware of it.


Ah... OK, I see. And yes, it does make a kind of sense, because CircularList is a template, which means the compiler presumably doesn't try and compile the template code at the point of inclusion, but at the point where the instantiation occurs.

If CircularList weren't a template, I suspect the compilation would fail, because the compiler would attempt to compile it at the point of inclusion, at which point no "using namespace" statement has occurred.

Although if that's all true, then I don't understand why it doesn't also apply to the second program the OP posted.

Thanks for setting me straight on the MS stuff - I'd obviously gotten confused on the details!


Those aliases in the global namespace only occur in the C library, so I don't think that's what is happening here. If one changes the code in main.cpp in the OP:

[...]

one gets the expected errors. It seems it uses whatever namespaces are visible at the point of instantiation, which makes an odd sort of sense, but probably isn't intentional and has the potential to introduce some puzzling issues if one isn't aware of it.


Ah... OK, I see. And yes, it does make a kind of sense, because CircularList is a template, which means the compiler presumably doesn't try and compile the template code at the point of inclusion, but at the point where the instantiation occurs.

If CircularList weren't a template, I suspect the compilation would fail, because the compiler would attempt to compile it at the point of inclusion, at which point no "using namespace" statement has occurred.

Although if that's all true, then I don't understand why it doesn't also apply to the second program the OP posted.

Thanks for setting me straight on the MS stuff - I'd obviously gotten confused on the details!

Lol. I have the same feeling.....
Topic archived. No new replies allowed.