请告诉我如何解决这个问题。不知道怎么回事,参数的值是预定义的,但是被认为是未声明的。(第29行出现错误)
#include <iostream>
#include <random>
#include <time.h>
#include <windows.h>
using namespace std;
int sortIncrease(int a[], const int length, bool IsOn = false) {
for (size_t i = 0; i < length; i++)
{
for (size_t j = 0; j < length; j++)
{
if (a[i] > a[j]) {
a[i] = a[j] + a[i];
a[j] = a[i] - a[j];
a[i] = a[i] - a[j];
}
}
cout << a[i];
}
}
int main() {
const int length = 10;
int UserChoice;
int a[length]={1,3,5,2,8,11,9,4,15,19};
cout << "Choose sorting metod:\n1-> increasing\n0->decreasing"<<endl;
cin >> UserChoice;
if (UserChoice == '1') {
cout << sortIncrease(a, length, IsOn = false);
}
}
首先,这样的挑战
false
意味着将同时分配给变量IsOn
的值作为第三个参数传递。IsOn
而你所在的变量main
实际上并没有被声明。为了更清楚:
在这里,在第二行之后,变量
x
得到值 2,以及 2y
的平方根。如果您想将它作为第三个参数传递
false
,则根本不能提及它,因为这是一个具有false
默认值的参数,但您也可以显式指定它。它们是两个相同的函数调用。
PS你使用的交换方法比使用临时变量的方法效率低,最重要的是,当总和超过可表示的值时,它可能不适用于变量的大值。