C++接收错误,该函数未在此范围内声明

标签 c++ arrays function sorting

<分区>

这是我正在为我大学的作业编写的程序。我已经编写了所有从用户那里获取 10 个整数的代码,并要求用户按 1 以按升序显示列表中的奇数整数或按 2 以按升序显示列表中的偶数整数。

嗯,我已经在程序的main()函数之前声明并定义了冒泡排序函数,后面在main()中使用了这个函数来对偶数和奇数进行升序排序。但是,即使我已经在顶部声明了该函数,我仍然收到该函数未在此范围内声明的错误。我尝试了所有可能的事情。请帮助我,我该怎么办?下面是我的代码

#include <iostream>
#include <conio.h>
using namespace std;

void BuubleSort_Function(int [], int);

void BuubleSort_Function(int arr[], int arrSize)
{
    int extraMem;

    for(int i = 0; i < arrSize; i++)
    {
        for(int arrIndex = 0; arrIndex < arrSize - 1; arrIndex++)
        {
            if(arr[arrIndex] > arr[arrIndex+1])
            {
                extraMem = arr[arrIndex];
                arr[arrIndex] = arr[arrIndex+1];
                arr[arrIndex+1] = extraMem;
            }
        }
    }
}

main()
{
    int num[10], i, even[10], odd[10], inputOpt, totalEvens = 0, totalOdds = 0;

    system("cls");

    cout << "Please enter 10 integers: " << endl;

    for(i = 0; i < 10; i++)
    {
        cin >> num[i];
    }

    cout << endl << endl << endl << "1. Show odd numbers in ascending order and their total numbers" << endl;
    cout << "2. Show even numbers in ascending order and their total numbers" << endl;

    do
    {   
        cout << endl << "Enter 1 for the first option or 2 for the second option: ";
        cin >> inputOpt;

        if(inputOpt != 1 && inputOpt != 2)
        {
            cout << "Wrong Input! Please enter the correct input value";
        }
    }
    while(inputOpt != 1 && inputOpt != 2);

    if(inputOpt == 1)
    {
        for(i = 0; i < 10; i++)
        {
            if(num[i] % 2 == 1)
            {   
                odd[totalOdds] = num[i];
                totalOdds++;
            }
        }

        BubbleSort_Function(odd,totalOdds);
        cout << endl << "The total numbers of Odds Integers are " << totalOdds;
        cout << endl << "The Integers arranged in Ascending Order:" << endl;

        for(i = 0; i < totalOdds; i++)
        {
            cout << odd[i] << "\t";
        }
    }

    if(inputOpt == 2)
    {
        for(i = 0; i < 10; i++)
        {
            if(num[i] % 2 == 0)
            {
                even[totalEvens] = num[i];
                totalEvens++;
            }
        }

        BubbleSort_Function(even,totalEvens);
        cout << endl << "The total numbers of Odds Integers are " << totalEvens;
        cout << endl << "The Integers arranged in Ascending Order:" << endl;

        for(i = 0; i < totalEvens; i++)
        {
            cout << even[i] << "\t";
        }
    }
}

最佳答案

一个简单的拼写错误:该函数被声明和实现为 BuubleSort_Function

您尝试使用 BubbleSort_Function 调用它。

编译器错误消息非常有用。一定要学会解释它们。

(最后,标准 C++ 要求您将 main() 标记为返回一个 int - C++ 编译器将插入一个隐式的 return 0进入 main 如果它丢失了。一些编译器 - 特别是用于嵌入式系统的编译器 - 放弃了这个要求,但这是与标准的偏差。)。

关于C++接收错误,该函数未在此范围内声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40889245/

相关文章:

mysql - 如何在mysql中为预先插入的记录插入唯一的id

c++ - 果酱 SDK 模拟器错误 : Couldn't initialize Direct Draw

c - 键盘模拟失败

javascript - 尝试将对象添加到 Javascript 对象数组时出现“意外标记 d”

java - 为什么不能使用 public static <T,U> T[] copyOf(U[] original,int newLength,Class<? extends T[]> newType) 将 Integer 数组转换为 String 数组

C++ 继承和虚函数

c++ - 鼠标点击逻辑嵌套多的if语句如何处理?

c++ - 是否存在检测有符号类型的位移位操作的 GCC 警告?

c++ - 递归模式 (C++)

javascript - 为什么有些变量没有定义?