为什么这段代码在字符串中给出错误不匹配
why this code giving error mismatch in strcpy
strcpy()
函数存在不匹配错误。我是c++语言的新手。
#include<iostream>
#include<cstring>
using namespace std;
#define max 5
class ss {
private:
char str[max][10];
public:
void get_str() {
for (int i = 0; i < max; i++)
cin >> str[i];
}
void disp() {
cout << "Entered strings aren";
for (int i = 0; i < max; i++) {
if (strcmp(str[i], str[i + 1]) != 0)
cout << str[i] << endl;
}
}
/*
void sort()
{
char temp[max];
for (int i = 0; i < max - 1; i++)
for (int j = 0; j < (max - i - 1); j++)
{
if (strcmp(str[j], str[j + 1])>0)
{
strcpy(temp, str[j]);
strcpy(str[j], str[j + 1]);
strcpy(str[j + 1], temp);
}
}
disp();
}
*/
void qsort()
{
qs(str, 0, max - 1);
disp();
}
void qs(char *&str, int st, int e)
{
int pi = part(str, st, e);
qs(s, st, pi - 1);
qs(s, pi + 1, e);
}
int part(char *&s, int st, int e)
{
char pi[max], swap[max];
strcpy(pi, s[e]);
int pii = st;
int i = st;
for (i; i < e; i++) {
if ((strcmp(s[i], s[pii])) <= 0)
{
strcpy(swap, s[pii]);
strcpy(s[pii], s[i]);
strcpy(s[i], swap);
pii++;
}
}
strcpy(swap, str[e]);
strcpy(str[e], str[pii]);
strcpy(str[pii], swap);
}
};
main()
{
ss s;
cout << "Enter the stringsn";
s.get_str();
s.disp();
s.sort();
cout << "after the sort" << endl;
s.disp();
}
我发现你的代码有几个问题:
- 参数被传递给
qs()
和part()
作为char *&
,而不是char **
或char [max][10]
。 -
s
在qs()
中没有定义(你是说str
吗?) - ISO c++禁止定义没有返回值的
main()
。 -
part()
具有非空返回类型,不返回值。
修复后的代码如下:
#include <iostream>
#include <cstring>
using namespace std;
#define max 5
class ss {
private:
char str[max][10];
public:
void get_str() {
for (int i = 0; i < max; i++)
cin >> str[i];
}
void disp() {
cout << "Entered strings aren";
for (int i = 0; i < max; i++) {
if (strcmp(str[i], str[i + 1]) != 0)
cout << str[i] << endl;
}
}
void sort()
{
char temp[max];
for (int i = 0; i < max - 1; i++)
for (int j = 0; j < (max - i - 1); j++)
{
if (strcmp(str[j], str[j + 1])>0)
{
strcpy(temp, str[j]);
strcpy(str[j], str[j + 1]);
strcpy(str[j + 1], temp);
}
}
disp();
}
void qsort()
{
qs(reinterpret_cast<char **>(str), 0, max - 1);
disp();
}
void qs(char **str, int st, int e)
{
int pi = part(reinterpret_cast<char **>(str), st, e);
qs(str, st, pi - 1);
qs(str, pi + 1, e);
}
int part(char **s, int st, int e)
{
char pi[max], swap[max];
strcpy(pi, s[e]);
int pii = st;
int i = st;
for (; i < e; i++) {
if ((strcmp(s[i], s[pii])) <= 0)
{
strcpy(swap, s[pii]);
strcpy(s[pii], s[i]);
strcpy(s[i], swap);
pii++;
}
}
strcpy(swap, str[e]);
strcpy(str[e], str[pii]);
strcpy(str[pii], swap);
// NO RETURN VALUE?
return 0;
}
};
int main()
{
ss s;
cout << "Enter the stringsn";
s.get_str();
s.disp();
s.sort();
cout << "after the sort" << endl;
s.disp();
return 0;
}
或者,如果你不想使用reinterpret_cast<>()
,你可以这样使用:
...
void qsort()
{
qs(str, 0, max - 1);
disp();
}
void qs(char str[max][10], int st, int e)
{
int pi = part(str, st, e);
qs(str, st, pi - 1);
qs(str, pi + 1, e);
}
int part(char s[max][10], int st, int e)
...
相关文章:
- 重载操作程序时出错>>用于类中的字符串 memebr
- C++ 创建包含链表和字符串的对象的链接列表时出错
- 从DLL导出函数,LoadLibrary()需要用TEXT转换的字符串才能编译而不会出错
- 在 Rcpp 中的字符串类型之间转换时出错
- 为什么 stl 字符串C++函数有时出错有时会出错?
- 读取字符串C++的字符时出错
- 读取字符串字符时出错 - 对于数组中的字符串值
- 通过字符串::插入插入字符时出错
- 使用来自串行端口的字符串数据来操作振镜扫描仪在更高的速度下会出错
- 通过 constexpr 中的 'const char *' glvalue 访问字符串文字的值会出错
- 读取字符串字符时出错
- 计算字符串中字符的出现次数时出错
- 在字符串中反转元音时出错
- C++ 使用 Strtok 读取字符串字符时出错
- 在 c++ 中调用将字符串作为参数传递的函数时出错
- 使用 C++Builder 项目读取字符串中的文件时出错
- 在字符串流中使用来自 github 的代码时出错
- C ++ ifstream 无法读取字符串,并且在读取字符时出错
- 需要帮助以了解字符串出错的地方
- cin和字符串出错