C++ 收到错误,指出函数未在此范围内声明

C++ Receiving error that the function was not declared in this scope

本文关键字:范围内 声明 函数 错误 C++      更新时间:2023-10-16

这是我为我大学的作业写的图片。我已经编写了从用户那里获取 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- 如果缺少隐式return 0,C++编译器会将隐式插入main。一些编译器 - 特别是嵌入式系统的编译器 - 放弃了这个要求,但这偏离了标准。

定义 -BuubleSort_Function

呼叫者 -BubbleSort_Function

将第5 行和第 7 行与第 64 行进行比较,您会发现问题所在。只是一个简单的错别字。