c++插入排序:在函数之间传递值时出错

C++ Insertion Sort: error when passing values between functions

本文关键字:出错 之间 插入排序 函数 c++      更新时间:2023-10-16

所以我在c++中为class编写这个程序,我要求用户输入数组的大小,然后输入数组中的数字。然后,它询问用户是希望按升序还是降序排序。

我已经单独测试了我的每个排序算法,两者似乎都完美地工作,然而,当我在sortArr函数中组合它们并传递用户从主函数输入的值时,我得到了一个可怕的错误,我甚至无法破译。有人知道我的程序有什么问题吗?

#include <iostream>
#include <stdio.h>
using namespace std;
void sortArr(bool order, int size)
{
    int imax;
    int max;
    int arr[size];
    if (order == true)
    {
        for (int i = size - 1; i > 0; i--)
        {
            max = arr[0];
            imax = i;
            for (int j = 0; j < i; j++)
            {
                if (arr[j] > arr[imax])
                {
                    max = arr[j];
                    imax = j;
                }
            }
            if (imax != i)
            {
                int temp = arr[i];
                arr[i] = arr[imax];
                arr[imax] = temp;
            }
        }
    }
    else if (order == false)
    {
        for (int i = 0; i < size; i++)
        {
            max = arr[0];
            imax = i;
            for (int j = size - 1; j > i; j--)
            {
                if (arr[j] > arr[imax])
                {
                    max = arr[j];
                    imax = j;
                }
            }
            if (imax != i)
            {
                int temp = arr[i];
                arr[i] = arr[imax];
                arr[imax] = temp;
            }
        }
    }
}
int main()
{
    int size;
    int a;
    bool order;
    string output;
    cout << "Enter the size of the array: ";
    cin >> size;
    if (size < 0)
    {
        cout << "ERROR: You entered an incorrect value for the array size!" << endl;
        return 1;
    }
    int arr[size];
    cout << "Enter the numbers in the array, seperated by a space, and press enter: ";
    for (int i = 0; i < size; i++)
    {
        scanf("%d", &arr[i]);
    }
    cout << "Sort in ascending (0) or descending (1) order? ";
    cin >> a;
    if (a == 0)
    {
        order = true;
        cout << "This is the sorted array in ascending order: ";
    }
    else if (a == 1)
    {
        order = false;
        cout << "This is the sorted array in descening order: ";
    }
    output = sortArr(order, size);
    cout << output << endl;
    return 0;
}
cout << "Sort in ascending (0) or descending (1) order? ";
cin >> a;
if(a == 0){
    order = true;
    cout << "This is the sorted array in ascending order: ";
}
else if(a == 1) {
    order = false;
    cout << "This is the sorted array in descening order: ";
}
output = sortArr(order, size);
cout << output << endl;
return 0;
}

我收到的错误代码是:

sortArray1.cpp: In function 'int main()':
sortArray1.cpp:84:9: error: no match for 'operator=' (operand types are 'std::string {aka std::basic_string<char>}' and 'void')
  output = sortArr(order, size);
         ^
sortArray1.cpp:84:9: note: candidates are:
In file included from /usr/include/c++/4.8/string:52:0,
                 from /usr/include/c++/4.8/bits/locale_classes.h:40,
                 from /usr/include/c++/4.8/bits/ios_base.h:41,
                 from /usr/include/c++/4.8/ios:42,
                 from /usr/include/c++/4.8/ostream:38,
                 from /usr/include/c++/4.8/iostream:39,
                 from sortArray1.cpp:1:
/usr/include/c++/4.8/bits/basic_string.h:546:7: note: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::operator=(const std::basic_string<_CharT, _Traits, _Alloc>&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]
       operator=(const basic_string& __str)
       ^
/usr/include/c++/4.8/bits/basic_string.h:546:7: note:   no known conversion for argument 1 from 'void' to 'const std::basic_string<char>&'
/usr/include/c++/4.8/bits/basic_string.h:554:7: note: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::operator=(const _CharT*) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]
       operator=(const _CharT* __s)
       ^
/usr/include/c++/4.8/bits/basic_string.h:554:7: note:   no known conversion for argument 1 from 'void' to 'const char*'
/usr/include/c++/4.8/bits/basic_string.h:565:7: note: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::operator=(_CharT) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]
       operator=(_CharT __c)
       ^
/usr/include/c++/4.8/bits/basic_string.h:565:7: note:   no known conversion for argument 1 from 'void' to 'char'

您有两个名为"arr"的变量:一个在main中,一个在sortArr中。

您正在读取main中的一个并对sortArr中的一个进行排序(这是未初始化的,因此程序是未定义的)。

传递数组作为参数:

void sortArr(bool order, int arr[], int size){

还有一个问题是,您试图将sortArr不存在的返回值(void)分配给std::string
不要那样做。

像这样调用函数:

sortArr(order, arr, size);

(int arr[size];也依赖于g++扩展-可变长度数组-这是非标准的。考虑使用std::vector。它已经存在了几十年了,再也没有什么好害怕的了。