【发布时间】:2021-11-29 16:48:09
【问题描述】:
我正在尝试对函数 bubbleSort(names, size) 进行冒泡排序,并使用我的驱动程序代码对其进行测试。调用该函数时,驱动程序代码出现错误。我想知道调用函数时如何正确设置第一个参数。
我收到两个与第一个参数“名称”有关的错误。
错误(活动)E0167 “char *”类型的参数与“char **”类型的参数不兼容
错误 C2664 'void bubbleSort(char *[],const int)': 无法将参数 1 从 'char [8]' 转换为 'char *[]'
我正在使用带有 Visual Studio Community 2019 的 Windows 10 Pro(64 位)。 提前感谢您的帮助。
#include <iostream>
#include <string.h>
using namespace std;
enum Bool { TRUE, FALSE };
void bubbleSort(char *names[], const int size)
{
Bool swapped;
char* temp;
for (int i = 0; i < size; ++i)
{
swapped = FALSE;
for (int j = 0; j < size - i - 1; ++j)
{
if (names[j] > names[j + 1])
{
temp = names[j];
names[j] = names[j + 1];
names[j + 1] = temp;
swapped = TRUE;
}
}
// if no two elements were swapped by inner loop, then break
if (swapped == FALSE)
{
break;
}
}
}
int main()
{
char names[] = {'s', 'a', 'c', 'd', 'i', 'd', 'm', 'o'};
int size = 8;
bubbleSort(names, size); // i get error here for 'names'
for (int i = 0; i < size; ++i)
cout << names[i] << " " << endl;
}
【问题讨论】:
-
您声明该函数采用
char**(与char*[]相同),但您传入的是char*(与char[]相同) - 就像错误已经告诉您一样方式!可能您应该更改参数类型以匹配您传入的内容! -
@CherryDT 我将参数类型从 *names[] 更改为 names[] 并将函数中的本地值从 *temp 更改为 temp 并摆脱了错误,我能够构建和运行。非常感谢您的建议。
标签: c++ visual-studio visual-c++