C++ 在数字常量之前需要不合格的 id?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15059810/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 18:57:52  来源:igfitidea点击:

Expected unqualified-id before numeric constant?

c++comma-operator

提问by Brandon

I'm getting errors on lines 102, 115, and 128. What am I doing wrong? It says:

我在行上遇到错误102, 115, and 128。我究竟做错了什么?它说:

Expected unqualified-id before numeric constant

数字常量前的预期非限定 ID

and I don't know what that means. I've tried to fix this for a week now, and it's due in my C++ class this coming Wednesday. I could really use some outside advice here. What am I doing wrong:

我不知道那是什么意思。我已经尝试解决这个问题一个星期了,它将于本周三在我的 C++ 课上到期。我真的可以在这里使用一些外部建议。我究竟做错了什么:

#include <iostream>
#include <fstream>
#include <iomanip>
#include <conio.h>
#include <string>

using namespace std;

double qMeter = 0;
double hMeter = 0;
double oneMeter = 0;
int solDay = 0;
string garbage;
string localTime;
string decSol;
ifstream input;
ofstream output;

//function prototypes
double low(double lowTemp);
double high(double highTemp);
float average(double avgTemp);

int main()
{

input.open("curiosity234x.dat"); //opens input data file
output.open("output.dat"); //opens output data file


  for (int i = 0; i < 4; i++) //gets rid of the first four lines
{
  getline(input,garbage);
  cout << endl;
}

while (!input.eof())
{
    int count; 
    double newOneMeter;
    double newHMeter;
    double newQMeter;

    if (solDay == 2) //processes data for the second solar day
    {
     input >> solDay >> localTime >> decSol 
     >> newOneMeter >> newHMeter >> newQMeter;

     oneMeter = oneMeter + newOneMeter;
     hMeter = hMeter + newHMeter;
     qMeter = qMeter + newQMeter;
     count++;

     output << solDay << fixed << setprecision(1) << setw(5)
        << "Solar" << "Average" << "Low" << "High"
        << "Average" << "Low" << "High" 
        << "Average" << "Low" << "High" 
        << "Day" << "Temp" << "Temp" << "Temp" << "Temp" << "Temp"
        << "Temp" << "Temp" << "Temp" << "Temp"
        << fixed << setprecision(15) << "1 meter" << ".5 meters"
        << ".25 meters"
        << average(oneMeter) << low(oneMeter) << high(oneMeter)
        << average(hMeter) << low(hMeter) << high(hMeter)
        << average(qMeter) << low(qMeter) << high(qMeter);
    }

    if (solDay == 3) //processes data for the third solar day
    {
     input >> solDay >> localTime >> decSol 
     >> newOneMeter >> newHMeter >> newQMeter;

     oneMeter = oneMeter + newOneMeter;
     hMeter = hMeter + newHMeter;
     qMeter = qMeter + newQMeter;
     count++;

     output << solDay << fixed << setprecision(1) << setw(5)
        << "Solar" << "Average" << "Low" << "High" 
        << average(oneMeter) << low(oneMeter) << high(oneMeter)
        << average(hMeter) << low(hMeter) << high(hMeter)
        << average(qMeter) << low(qMeter) << high(qMeter);
    }
  }

  cout << endl << "The output.dat file has been written and transmitted.";
  /*
  reads first line. Assigns first string to 'int solDay' 
  second to 'string time', third to decSol, fourth to oneMeter, 
  fifth to hMeter and sixth to qmeter. Meters should have setw().
  */
getch();
return 0;

input.close();
output.close();
}


//functions used in main
double low(double lowTemp)
{
   int test = 10,000;
   double least;

   if (lowTemp < test)
   {
     lowTemp = test;
     lowTemp = least;       
    }
    return least;
}

double high(double highTemp)
{
   int test = 10,000;
   double most;

   if (highTemp < test)
   {
      highTemp = test;
      highTemp = most;        
    }
   return most;
}

float average(double avgTemp)
{
   avgTemp = avgTemp / count;
   return avgTemp;
}

采纳答案by Shafik Yaghmour

Errors on line 102 and 115 are due to the fact that you have a commain 10,000, but it should be 10000, the commacan not be used as a numeric separator. What you are actually using is the comma operatorwhich evaluates it's expressions from left to rightand only returns the last one. So in this case:

上线102和115错误是由于这样的事实,你有一个逗号10,000,但应10000中,逗号不能用作数字分隔符。您实际使用的是逗号运算符,它从左到右计算它的表达式,并且只返回最后一个。所以在这种情况下:

int test = 10,000;
           ^  ^
           |  Expression 2
           Expression 1

since =has higher precedence it happens first and your are left with a syntax error, on the other hand:

因为=具有更高的优先级,所以它首先发生,另一方面,您会遇到语法错误:

int test = (10,000);

would have resulted in the value 10being discarded and octal literal000being assigned to test.

将导致该值10被丢弃并将八进制文字000分配给test.

The final error on line 128 is due to the fact that count is local to mainbut you are trying to use it in average, you need to pass countas a second paramter.

第 128 行的最后一个错误是由于 count 是本地的,main但您试图在 中使用它average,您需要count作为第二个参数传递。

回答by Joel

I searched this error when I ran into it. The cause of my troubles? I was trying to give a class the same name as #define value that had been included into my project from a 3rd party API header. Trying a different class name, I got it to compile!

当我遇到它时,我搜索了这个错误。我烦恼的原因是什么?我试图给一个与#define 值相同的名称,该值已从 3rd 方 API 标头包含在我的项目中。尝试不同的类名,我编译成功了!