C ++插入排序:在函数

时间:2015-09-30 22:02:17

标签: c++ sorting insertion-sort

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

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

#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'

1 个答案:

答案 0 :(得分:2)

您有两个名为&#34; arr&#34;的变量:main中的一个和sortArr中的一个。

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

将数组作为参数传递:

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

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

像这样调用函数:

sortArr(order, arr, size);

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