为什么这段代码在字符串中给出错误不匹配

why this code giving error mismatch in strcpy

本文关键字:字符串 出错 不匹配 错误 段代码 代码 为什么      更新时间:2023-10-16

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();
}

我发现你的代码有几个问题:

  1. 参数被传递给qs()part()作为char *&,而不是char **char [max][10]
  2. sqs()中没有定义(你是说str吗?)
  3. ISO c++禁止定义没有返回值的main()
  4. 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)
...