错误无法在赋值中将"std::string {aka std::basic_string<char>}"转换为"char" - C++

Error cannot convert 'std::string {aka std::basic_string<char>}' to 'char' in assignment - C++

本文关键字:string char std lt gt C++ 转换 aka 赋值 basic 错误      更新时间:2023-10-16

所以我在代码行中得到了上面提到的错误:"women[count_wc]=(temp(;"[Error]无法在赋值-C++中将"std::string{aka std::basic_string}"转换为"char">

它在被调用的函数内部。

此外在实际调用函数的地方发现了另一个错误。处的错误"get_comp_women(women,MAX_W,array,ROW(;"是[Error]无法将"(std::string*((&women("从"std::string*{aka std::basic_string*}"转换为"std:string{akastd::asic_string}">

const int MAX_W = 18;
const int MAX_T = 18;
const int MAX_E = 14;
const int ROW = 89;
using namespace std;
struct data
{
    string name;
    string event;
};

void get_comp_women(string women, int MAX_W, data array[], int ROW)
{
    int count_wc = 0;
    int count_wn = 0;
    int event_occ = 0;
    string temp;
    temp = (array[0].name);
    event_occ = (ROW + MAX_W);

    for (int i = 1; i < event_occ; i++)
    {
        if (temp == array[count_wn].name)
        {
            women[count_wc] = (temp);
            count_wn++;
        }
        else
        {
            temp = array[count_wn].name;
            count_wc++;
        }
    }
int main()
{
    string women[MAX_W];
    data array[ROW];
    get_comp_women(women, MAX_W, array, ROW);
}

您的函数接受women作为std::string,而您需要一个数组,因此,在函数中women[count_wc]的意思是"字符串中的字符",而不是"字符串数组中的字符串">

women[count_wc] = (temp);
____________/    ____/
   ^                 ^-----std::string   
   ^--- one character in the string

您需要更改函数签名,使其接受std::string[]而不是std::string:

void get_comp_women(string women[], int MAX_W, data array[], int ROW)

您得到的第二个错误是不言自明的,也就是说(试图将数组传递到等待字符串的函数中(。

void get_comp_women(string women, int MAX_W, data array[], int ROW)

应该成为

void get_comp_women(string women[], int MAX_W, data array[], int ROW)

函数的调用及其内部的逻辑都需要一个数组。