错误:从'char'到'const char *'的转换无效

时间:2014-01-11 06:27:53

标签: c++

我想创建可以返回两个新字符串的函数,该字符串是旧字符串的组合但是我收到了上述错误。

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

错误位于行f_num.append(num.at(pos_be))以及我使用的其他行附加字符串。有谁想知道这里出了什么问题?

2 个答案:

答案 0 :(得分:1)

这里的问题是,at函数返回一个char而不是一个字符串。但是append函数支持一个字符串。所以你得到这个错误。在追加之前将char转换为字符串。

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

答案 1 :(得分:0)

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