【问题标题】:Just Want to know what this error message really means!只是想知道此错误消息的真正含义!
【发布时间】:2009-11-12 00:29:27
【问题描述】:

'{'之前的预期不合格ID

我的代码中的这个错误在哪里?谢谢大家!

#include <iostream>

using std::cout;
using std::endl;

//function prototypes
void findsmallest (int[], int &); 
void findsmallest  (int scores [], int & min);

int main()
{
    //declare variables
    int smallest = 0;
    int scores[20] = { 90, 54, 23,  75, 67,
                       89, 99, 100, 34, 99, 
                       97, 76, 73,  72, 56, 
                       73, 72, 65,  86, 90 };
    findsmallest(scores, smallest);                   
    return 0;

    //call function find smallest
    findsmallest (scores, smallest);                   

    cout << "Minimum value in the array is " << smallest << endl;

    system ("pause");
    return 0;
}

//function definition

void findsmallest(int scores [], int & min);
{

    min = scores[0];
    int i;

    for(i = 0; i < 20; i++)
    {
        if(scores[i] < min)
        {
            min = scores[i];
        }
    }
}


//end display findsmallest
system ("pause");
return 0;

【问题讨论】:

  • 您的问题是什么?除了//declare array,你的代码已经声明了一个数组。
  • 我看到评论是格式问题。你所拥有的已经是正确的。
  • “我到底应该怎么做”。究竟要做什么
  • 考虑你的数组声明。
  • 我看到你已经进行了更正,所以你已经是正确的,除非你没有很好地传达你的更正

标签: c++ arrays


【解决方案1】:

错误在findsmallest() 函数定义的第一行。去掉分号,它应该可以工作(除非代码中出现其他错误——我没有检查它的正确性)。

void findsmallest(int scores [], int & min); <-------semicolon
{

void findsmallest(int scores [], int & min) <--------no semicolon
{

错误告诉您分号后面的左大括号 ({) 缺少前面的类/结构/联合/函数声明,因此编译器不知道如何处理它。删除分号,现在编译器知道它是函数定义的主体。

【讨论】:

  • 这也是我最初发现的
  • @d03boy--很好的编辑。比我原来写的清楚多了!
  • @d03boy - 非常感谢您澄清错误消息,是的,我删除了分号。谢谢德鲁,这里有一群非常乐于助人和专业的人!
【解决方案2】:

以下代码声明了一个长度为 5 的 int 数组。

int billy [5] = { 16, 2, 77, 40, 12071 }; 

Here 是学习 C++ 数组的绝佳教程。

【讨论】:

  • 谢谢您查看链接。
【解决方案3】:

你想做什么?第一行看起来像是以注释开头

//declare array

然后继续进行数组声明。您还想完成什么?

【讨论】:

    【解决方案4】:

    嗯,你想找到最小的并打印出来,对吧?

    为此,您必须考虑数组的每个成员,并跟踪找到的最小成员。在考虑了所有数组值之后,您将知道哪一个是最低的,因此您可以打印它。在伪代码中:

    var smallestSoFar = aBigNumber
    
    for i in array loop
        if array[i] < smallestSoFar then
            smallestSoFar = array[i]
        end if
    end loop
    print smallestSoFar
    

    希望对你有帮助;)

    【讨论】:

    • 让我了解预期的内容。谢谢!
    • @Rosemary:在这种情况下,您应该将此答案标记为已接受。只需点击答案旁边的复选标记即可。
    • 很高兴您发现它有帮助!!另一个提示:aBigNumber,在 C 中,应该是 INT_MAX。 INT_MAX 是可能的最大整数,使用该常量只需包含 。祝你好运!!
    【解决方案5】:

    要查找数组中的最小值,您可以执行以下操作:

    //declare array:
    int scores[20] = {90, 54, 23, 75, 67, 89, 99, 100, 34, 99, 
        97, 76, 73, 72, 56, 73, 72, 65, 86, 90};
    
    /* give the variable 'smallest' a high value -
       higher than any in the array:
    */
    int smallest = 9999;
    
    /* inspect each value in the array and if it is greater than
       the value of 'smallest', set smallest to that value:
    */
    int i;
    for (i = 0; i < 20; ++i)
        if (scores[i] < smallest)
            smallest = scores[i];
    

    哦,看问题完全变了,现在我的答案看起来完全疯了:-/

    猜你喜欢
    • 2012-11-03
    • 2021-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-30
    • 1970-01-01
    相关资源
    最近更新 更多