MakaraPr
MakaraPr

Reputation: 171

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?

Upvotes: 0

Views: 1523

Answers (2)

Aswin Murugesh
Aswin Murugesh

Reputation: 11070

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

Upvotes: 1

crastinus
crastinus

Reputation: 133

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

Upvotes: 0

Related Questions