C++ 错误:表达式必须具有整数或无作用域的枚举类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21341254/
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: Expression must have integral or unscoped enum type
提问by user3233328
#include <iostream>
#include <iomanip>
#include <string>
#include <algorithm>
#include <sstream>
using namespace std;
int main(){
float size;
float sumNum = 0;
float maxNum, minNum;
float mean;
float totalDev = 0;
float devSqr = 0;
float stdDev;
//Create a user input size
std::cout << "How many number would you like to enter? ";
std::cin >> size;
float *temp = new float[size];
//Getting input from the user
for (int x = 1; x <= size; x++){
cout << "Enter temperature " << x << ": ";
cin >> temp[x];
}
//Output of the numbers inserted by the user
cout << endl << "Number --- Temperature" << endl << endl;
for (int x = 1; x <= size; x++){
cout << " " << x << " --- " << temp[x] << endl;
sumNum = sumNum + temp[x];
}
//Calculating the Average
mean = sumNum / size;
maxNum = minNum = temp[1];
for (int x = 1; x <= size; x++){
if (maxNum < temp[x]){
maxNum = temp[x];
}
if (minNum > temp[x]){
minNum = temp[x];
}
}
//Calculating Sample Standard Deviation
for (int x = 1; x <= size; x++){
totalDev = totalDev + (temp[x] - mean);
devSqr = devSqr + (pow((temp[x] - mean), 2));
}
stdDev = sqrt((devSqr / (size - 1)));
cout << endl << "The sum: " << sumNum << endl; //the sum of all input
cout << "The mean: " << mean << endl; //calculate the average
cout << "Maximum number: " << maxNum << endl; // print biggest value
cout << "Minimum number: " << minNum << endl; // print smallest value
cout << "The range between the maximum and the minimum: " << maxNum - minNum << endl; //the range
cout << "Deviation: " << totalDev << endl;
cout << "The squares of deviation: " << devSqr << endl;
cout << "The Standard Deviation: " << setprecision(1) << fixed << stdDev << endl;
system("pause");
}
I want to get the size of the array from the user, but when I'm using (float *temp = new float[size];
), I got an error "expression must have integral or unscoped enum type." When I input the number, it working nicely up until to the range number. After that, start from deviation to the standard deviation, the calculation all messed up.
我想从用户那里获取数组的大小,但是当我使用 ( float *temp = new float[size];
) 时,出现错误“表达式必须具有整数或无作用域枚举类型”。当我输入数字时,它可以很好地工作直到范围数字。之后,从偏差开始到标准偏差,计算全乱了。
If I use int
for the 'size' and keep the 'temp' as float
, it gave me different error.
如果我使用int
'size' 并将 'temp' 保持为float
,它会给我不同的错误。
How can I fix this?
我怎样才能解决这个问题?
回答by Reed Copsey
Your variable size
is declared as: float size;
您的变量size
声明为:float size;
You can't use a floating point variable as the size of an array - it needs to be an integer value.
您不能使用浮点变量作为数组的大小 - 它必须是整数值。
You could cast it to convert to an integer:
您可以将其转换为整数:
float *temp = new float[(int)size];
Your other problem is likely because you're writing outside of the bounds of the array:
您的另一个问题可能是因为您在数组范围之外写入:
float *temp = new float[size];
//Getting input from the user
for (int x = 1; x <= size; x++){
cout << "Enter temperature " << x << ": ";
// cin >> temp[x];
// This should be:
cin >> temp[x - 1];
}
Arrays are zero based in C++, so this is going to write beyond the end and never write the first element in your original code.
数组在 C++ 中是基于零的,所以这将写到最后并且永远不会写原始代码中的第一个元素。