Question about a TICPP example# Programming - 葵花宝典
m*e
1 楼
这是TICPP中的一个example
#include
#include
#include
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]; // Exhausts memory
}
} ///:~
但编译居然报错:
g++ -c NewHandler.cpp
NewHandler.cpp: In function `void out_of_memory()':
NewHandler.cpp:16: error: `count' undeclared (first use this function)
#include
#include
#include
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]; // Exhausts memory
}
} ///:~
但编译居然报错:
g++ -c NewHandler.cpp
NewHandler.cpp: In function `void out_of_memory()':
NewHandler.cpp:16: error: `count' undeclared (first use this function)