C++ 错误 Nullptr 未声明
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22903250/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error Nullptr was not declared
提问by user3502479
So the nullptr error doesnt show up when i compile it at school and i think i can fix it with adding a line when i compile it, is there another way to get rid of it, and the other two errors i dont understand why im getting them at all. Can someone explain at least the nullptr error?
所以当我在学校编译它时 nullptr 错误没有出现,我想我可以通过在编译时添加一行来修复它,有没有另一种方法可以摆脱它,另外两个错误我不明白为什么我得到他们根本。有人可以至少解释一下 nullptr 错误吗?
main.cpp: In function 'int main()':
main.cpp:在函数“int main()”中:
error: 'array' was not declared in this scope
错误:未在此范围内声明“数组”
error: 'hours' was not declared in this scope
错误:未在此范围内声明“小时”
error: 'nullptr' was not declared in this scope
错误:未在此范围内声明“nullptr”
int main()
{
float *studentData;
int numStudents;
int size;
int average = getAverage(*array, *hours);
int median = getMedian(hours);
int mode = getMode(hours);
cout << "How many students were surveyed? ";
cin >> numStudents;
studentData = makeArray(numStudents);
if (studentData == nullptr)
cout << "Error allocating memory.";
else
{
getFBData(studentData, numStudents);
selectionSort(studentData, numStudents);
for (int i = 0; i < numStudents; i++)
cout << *(studentData + i) << endl;
delete[] studentData;
}
getAverage(*array, hours);
printArray(size, hours);
getMedian(*array, hours);
getMode(*array, hours);
cout << "STATISTICS " << endl;
cout << "\n Mean: " << average;
cout << "\n Median: " << median;
cout << "\n Mode: " << mode;
return 0;
}
回答by M.M
On this line:
在这一行:
int average = getAverage(*array, *hours);
you refer to array
and hours
, however you have not declared those things yet. The "school version" of the code must have been different.
你指的是array
和hours
,但是你还没有声明那些东西。代码的“学校版本”一定是不同的。
Re. the nullptr
error: that was added to C++ in 2011. Perhaps the school has up-to-date compilers but you have an older compiler at home. If you change nullptr
to 0
it will be fine.
关于。的nullptr
错误:那是在2011年加入到C ++也许学校有上最新的编译器,但你在家里有一个较旧的编译器。如果您更改nullptr
到0
会被罚款。
回答by raju
The easiest way to solve this problem is to change nullptr to 0. Though not all the time this works. But it can be a small code solution.
解决此问题的最简单方法是将 nullptr 更改为 0。尽管并非总是如此。但它可以是一个小代码解决方案。
You can also use -std=c++11 parameter while compiling using g++. So the compiling command in the terminal will be : g++ "your file" -std=c++11
您还可以在使用 g++ 编译时使用 -std=c++11 参数。所以终端中的编译命令将是:g++ "your file" -std=c++11