简体   繁体   中英

Error: invalid conversion from 'char' to 'const char*'

I want to make function that can return two new string that is the composition of the old one but I got an above error.

string constru(string num, int pos_be, int pos_end)
{
    string f_num="";
    string s_num="";
    f_num.append(num.at(pos_be));
    f_num.append(num.at(pos_end));
    num.erase(pos_be);
    num.erase(pos_end);
    for(int i=0; i<num.size();i++)
    {
        s_num.append(num.at(i));
    }
return f_num,s_num;
}

The Error is at the line f_num.append(num.at(pos_be)) as well as the other lines that I used append with string. Does anyone want know what went wrong here?

The problem here is, the at function returns a char and not a string. But the append function supports a string. So you get this error. Convert the char to string before you append.

f_num.append(std::string(num.at(pos_be)));
f_num.append(std::string(num.at(pos_end)));
f_num.append(std::string(num.at(pos_be)));
f_num.append(std::string(num.at(pos_end)));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM