C ++插入排序:在函数之间传递值时出错
C++ Insertion Sort: error when passing values between functions
所以我正在用 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
的不存在的 return 值 (void
) 分配给 std::string
。
不要那样做。
像这样调用函数:
sortArr(order, arr, size);
(int arr[size];
也依赖于 g++ 扩展——可变长度数组——这是非标准的。考虑使用 std::vector
。它已经存在了几十年,没有什么可怕的没有了。)
所以我正在用 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
的不存在的 return 值 (void
) 分配给 std::string
。
不要那样做。
像这样调用函数:
sortArr(order, arr, size);
(int arr[size];
也依赖于 g++ 扩展——可变长度数组——这是非标准的。考虑使用 std::vector
。它已经存在了几十年,没有什么可怕的没有了。)