【问题标题】:error C2059: syntax error : ']', i cant figure out why this coming up in c++错误 C2059:语法错误:']',我不知道为什么会出现在 C++ 中
【发布时间】:2010-04-29 04:12:22
【问题描述】:
void display_totals();
int exam1[100][3];// array that can hold 100 numbers for 1st column
int exam2[100][3];// array that can hold 100 numbers for 2nd column
int exam3[100][3];// array that can hold 100 numbers for 3rd column 
int main()
{
    int go,go2,go3;
    go=read_file_in_array;
    go2= calculate_total(exam1[],exam2[],exam3[]);
    go3=display_totals;
    cout << go,go2,go3;
    return 0;
}
void display_totals()
{

    int grade_total;
    grade_total=calculate_total(exam1[],exam2[],exam3[]);
}   
int calculate_total(int exam1[],int exam2[],int exam3[])
{
    int calc_tot,above90=0, above80=0, above70=0, above60=0,i,j;
    calc_tot=read_file_in_array(exam[100][3]);
    exam1[][]=exam[100][3];
    exam2[][]=exam[100][3];
    exam3[][]=exam[100][3];
    for(i=0;i<100;i++);
        {
            if(exam1[i] <=90 && exam1[i] >=100)
                {
                    above90++;
                    cout << above90;
                }
        }
        return exam1[i],exam2[i],exam3[i];

}

int read_file_in_array(int exam[100][3])
{
  ifstream infile;  

  int num, i=0,j=0;
  infile.open("grades.txt");// file containing numbers in 3 columns
    if(infile.fail()) // checks to see if file opended
    {
        cout << "error" << endl;
    }
  while(!infile.eof()) // reads file to end of line
      {
          for(i=0;i<100;i++); // array numbers less than 100
          {
            for(j=0;j<3;j++); // while reading get 1st array or element
            infile >> exam[i][j];
            cout << exam[i][j] << endl;
          }
      }
  infile.close();
  return exam[i][j];
}

【问题讨论】:

  • 错误错误?这听起来比正常错误要严重得多 :-)
  • 30-33, 27 是我遇到问题的行

标签: c++ arrays increment


【解决方案1】:

您传递给 calculate_total 的数据类型是错误的。 C++ 将其视为指向 int 的指针。你传入一个二维数组。您必须使 calculate_total 函数的输入类型与数组的类型相匹配。

此外,所有这些额外的 [] 都是无效的语法。传入定义为数组的变量时,只传入变量名。

// Invalid function call
f(myArray[]);

// Valid function call
f(myArray);

在实际功能内部,您要做什么?您是否尝试将exam1、exam2 和exam3 的元素修改为exam[100][3] 的值?

您还缺少数组int exam[100][3] 的声明。我在你的代码中没有看到它。

在calculate_total 的返回中,你的return 语句格式不正确。你只能返回一个值,不像 Python 那样会返回一个包含三个元素的元组。

【讨论】:

  • return 语句不是格式错误的,它只是可能没有达到预期的效果。它使用逗号运算符并简单地丢弃除最后一个值之外的所有值。
  • 我试图从我读取的文本文件中对数组中的数字进行排序。比如100-90是A,90-80是B等等。然后转到下一列,做同样的事情,然后是第三列,把所有的 A、B、C、D、F 加起来,最后在屏幕上打印出来
  • 我应该使用 i 和 j 而不是 100 和 3
【解决方案2】:

我在您的代码中观察到以下问题

  1. read_file_in_array 需要括号。 去=read_file_in_array; //无效的函数调用

  2. 将数组作为参数传递

  3. display_totals 需要括号

  4. 一开始就缺少函数原型

  5. display_totals 不会返回任何内容。但是你将它分配给一个变量

  6. 我不明白这个 calculate_total 函数在做什么。

如果这是您的原始代码,则此代码存在很多问题。我按原样使用此代码并使用 Turbo c++ 编译器进行编译。我有大约 24 个错误。

您能否重构您的代码并编译它。

【讨论】:

  • 我得到这些错误 C2059: syntax error : '] 我不知道为什么
  • 这是你的完整源代码吗?如果不能,您能否分享您收到此错误的确切代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-10
  • 2021-11-13
  • 2016-10-03
  • 2017-09-02
  • 1970-01-01
相关资源
最近更新 更多