c++ - 只是想知道此错误消息的真正含义!

标签 c++ arrays

expected unqualified-id before '{'

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

#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;

最佳答案

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

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

对比

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

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

关于c++ - 只是想知道此错误消息的真正含义!,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1719055/

相关文章:

c# - linq按子字符串对数组中的字符串进行排序

perl - 如何在 Perl 中以完全相同的方式对两个数组进行洗牌?

c++ - C++中while循环中的浮点错误

c++ - 链中的最后一个任务应该返回什么 - C++ Rest sdk?

C++模板如果匹配类型则调用成员函数否则抛出异常?

c++ - vector::clear:内存问题

javascript - 从两个对象数组中过滤,然后合并它们

c++ - 无法在 cpp 中绑定(bind)运算符 <<

c++ - 将 2 位数字的数组转换为整数 (C++)

java - 完成数组的第一个元素