Example of using namespace std error

As everybody would say, using std:: is usually better than using namespace std;.
I got the point on how it works but can you give me a code(using namespace std) that if compiled and run, will produce errors or some ambiguities?
1
2
3
4
5
#include <algorithm>

using namespace std;

int count;


The real problems don't come up until you've dumped several namespaces into global scope and your compiler is threatening to murder you with errors.

EDIT:
code [...] that if compiled and run, will produce errors or some ambiguities?
Code that is ambiguous will never compile. The defining characteristic of programming languages is that messages written in them have to be unambiguous to be correct.
If there are ambiguities in the code, it will just not compile. It won't give incorrect results because it will just never get that far.
Last edited on
The defining characteristic of programming languages is that messages written in them have to be unambiguous to be correct.
Thanks for correcting. I used to think that those code will continue to compile and run, and the error will just show in the actual process.

Although this help me to acquire new concept, I still find it hard to understand.
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
#include <iostream>
#include <algorithm>
#include <cstring>

// Count the number of characters before c
int count(const char* f, const char* t, char c)
{
	size_t i;
	for(i = 0; f < t and *f != c; ++i, ++f);
	return i;
}

int main()
{
	char* line = "Counter Test";

	size_t len;

	len = count(line, line + strlen(line), 'T');
	std::cout << "len = " << len << std::endl;

	using namespace std; // Poison the global namespace

	len = count(line, line + strlen(line), 'T');
	std::cout << "len = " << len << std::endl;

	return 0;
}
len = 8
len = 1
Topic archived. No new replies allowed.