函数未在此范围内声明?

时间:2018-02-02 08:09:54

标签: c++

我试图根据输入编写一个显示星号和空格的程序,并且我遇到了编译问题:

chart.cpp:24:41: error: ‘find_largest’ was not declared in this scope
  int largest = find_largest(values, size);

这是我的代码:

/*
 * Project 1
 * Author: Erik Ingvoldsen
 * Date: 2/1/2018
 */

#include <iostream>
using namespace std;

int size = 0; //initalizing "size" at 0.
const int MAX = 100; //setting max value
int values[MAX]; //100 int limit

int main(){
    int num;
    for (int i = 0; i < MAX; i++) {
        cin >> num; //allow the user to put in a number
        values[i] = num; //assigning value to the array
        if (num <= 0) {
            break; //stop if "0" or lower is entered
        }
        size++; //increase the size of array, assuming the for loop hasn't been broken
    }
    int largest = find_largest(values, size); //setting the amount of rows
    for (int i = 0; i < size; i++) {
        if (values[i] = largest) {
            cout << "*"; //if the value of the area reachest the highest row, give a *
        } else {
            cout << "\n"; //otherwise just give a blank space
        }
        largest--; //by shrinking "largest", we move down the next row
        cout << endl;
    }
    return 0;
}

int find_largest(int values[], int size) {
    int largest = 0;
    for (int i = 0; i < size; i++) {
        if (values[i] > largest) { 
            largest = values[i]; //if the value of the array is bigger than the current largest it is replace
        }
    }
    return largest; //once the for loop is completed, it returns the largest number found
}

我真的无法区分这个和函数之间的区别......我很确定我不应该将它声明为&#34; int largest = find_largest(values [],尺寸);&#34;

1 个答案:

答案 0 :(得分:3)

您应该在使用之前声明所有功能。简单的方法是使用原型。

// prototype
int find_largest(int values[], int size);

int main(){
    ...
}

int find_largest(int values[], int size) {
    ...
}