c++ - 函数未在此范围内声明?

标签 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
}

我真的分不清这个函数和函数之间的区别...而且我很确定我不应该将它声明为“int largest = find_largest(values[], size);”

最佳答案

您应该在使用函数之前声明所有函数。做到这一点的简单方法是使用原型(prototype)。

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

int main(){
    ...
}

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

关于c++ - 函数未在此范围内声明?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48578351/

相关文章:

python - 当 python 使用 "Python.h"调用该 c++ 进程时,如何在 python 中停止 c++ 进程

c++ - 嵌套类型作为基类的模板参数

c++ - 将 std::list 扩展为循环列表

c++ - tink_core Future (haxe) 形式 c++

c++ - 无法为 vector 的 vector 解析 Eclipse 方法

c++ - 内存分配 char* 和 char[]

c++ - GNU C++ : unique_ptr. h 没有那个文件或目录

c++ - 解释指针的这种情况

c++ - 提升 : having an array of ints and some special int how to find nearest to yours in array?

c++ - 将输入数组与字符串进行比较