簡體   English   中英

錯誤無法轉換 &#39;std::string {aka std::basic_string<char> }&#39; 到 &#39;char&#39; 賦值 - C++

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

所以我在代碼行中得到了上面提到的錯誤:“women[count_wc] = (temp);” [錯誤] 無法在賦值中將“std::string {aka std::basic_string}”轉換為“char” - C++

這是被調用的函數內部。

也是在該功能實際上是被稱為地方發現了另一個錯誤。 “get_comp_women(women, MAX_W, array, ROW);”處的錯誤是 [錯誤] 無法將 '(std::string*)(& women)' 從 'std::string* {aka std::basic_string*}' 轉換為 'std::string {aka std::basic_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)

函數的調用和其中的邏輯都需要一個數組。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM