Is this a problem of Visual Stdio?

I wanna try to use set_new_handler and write the code in the following. However when I compile it, the compiler says count in line 10 and 18 are ambiguous symbols. Why?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include "stdafx.h"
#include <iostream>
#include <new>
#include <cstdlib>
using namespace std;

int count = 0;

void out_of_memory() {
	cerr << "memory exhausted after " << count << " allocations!" << endl;
	exit(1);
}

int main() {
 set_new_handler(out_of_memory);
 while (1)
 {	 
	 count++;
	 new int[1000];
 }
} ///:~
Last edited on
I think that it is the result of using the using directive

using namespace std;

In future never use it.

Try substitute in these two statements count to ::count
This is one of the problems when using using namespace std;. It doesn't know if you mean ::count defined on line 7 or std::count defined in <algorithm>.
http://en.cppreference.com/w/cpp/algorithm/count
Topic archived. No new replies allowed.