C2664 :当我使用 str.append(str[i]) 时,发生错误

C2664 :when I use str.append(str[i]),error happening

本文关键字:str 错误 C2664 append      更新时间:2023-10-16
    string s1;
    string s2;
    getline(cin >> fixed, s);
    for (int j = 0; j < s.length(); j++)
    {
        if ((j & 1) == 0)
        {
            s1.append(s[j]);//the type of s[j] is char,but the error still 
                            //happening,
            cout << tmp << endl;
        }
        else
        {
            s2.append(s[j]);
        }
    }
    cout << s1 << " " << s2 << endl;

C2664"std::basic_string,std::分配器> &std::basic_string,std::分配器>::append(const std::basic_string,std::allocator> &(":无法将 elem 1 形式的 "char" 转换为 "std::initializer_list<_Elem>">

没有需要单个charappend()过载。看看这里:

http://en.cppreference.com/w/cpp/string/basic_string/append

只有一个重载需要单个char

basic_string& append( size_type count, CharT ch );

因此,您可以使用它:

 s1.append(1, s[j]);

或者,改用operator+=

s1 += s[j];