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
不存在的返回值(void
)分配给std::string
。
不要那样做。
像这样调用函数:
sortArr(order, arr, size);
(int arr[size];
也依赖于g++扩展-可变长度数组-这是非标准的。考虑使用std::vector
。它已经存在了几十年了,再也没有什么好害怕的了。
相关文章:
- C++中std::resize(n)和std::shrink_to_fit之间的区别
- int(c) 和 c-'0' 之间的区别。C++
- 访问者访问变体并返回不同类型时出错
- 在Linux for Windows上编译C++代码时出错
- 读取文件的最后一行并输入到链接列表时出错
- 重载操作程序时出错>>用于类中的字符串 memebr
- 调用专用模板时出错"no matching function for call to [...]"
- 在cuda线程之间共享大量常量数据
- C++-试图将函数指针推回到另一个CPP文件中的矢量时出错
- LINK 尝试使用 OpenSSL evp aes 256 c++ 时出错
- 在c代码之间共享数据的最佳方式
- Mix_Init和Mix_OpenAudio SDL之间的区别是什么
- 在Google Kick start中提交时出错
- C++ 使用 assign 函数的字符串与直接使用 '=' 更改值的字符串之间的区别
- 在c++中访问int到类对象的映射时出错
- 在 Rcpp 中的字符串类型之间转换时出错
- 英特尔 TBB,parallel_pipeline,当我在阶段之间传递常量字符*时出错
- 数值基之间的转换出错
- c++插入排序:在函数之间传递值时出错
- 在C++中查找两个项目之间的差异时出错