C ++错误,编译器无法识别string :: push_back

时间:2017-02-24 19:21:48

标签: c++ string c++11 compiler-errors

它遇到问题的功能:

string encode (string message, string key) {

    string code = "whatever";
    string forst;
    int num;
    string::size_type begin = 0;

    message = lower_and_strip(message);

    for (char val : message) {
        num = return_encoded_char(key, begin, val);
        forst = to_string(num);
        code.push_back(forst); //*******************************
    }


    return code;
}

明星界线就是它的意思。 return_encoded_char函数返回一个整数。

具体错误是 proj05.cpp:68:23: error: no matching function for call to 'std::basic_string<char>::push_back(std::string&)'并指向我加注的主题。

我最初刚刚声明code而没有初始化它,但改变它并没有解决它。我能找到的所有类似问题都有其他一些因素需要责备;我觉得这应该是相对简单的,但显然不是因为它不起作用。

我有#include <stream>using std::to_string等我正在使用-std = c ++ 11来编译它。

帮助。

P.S。在Linux上使用Geany。

1 个答案:

答案 0 :(得分:4)

您的code变量是std::stringstd::string类没有push_back()方法,需要另外std::string作为输入。您应该尝试使用+=运算符,它接受字符或字符串:

string encode (string message, string key) {

    string code = "whatever";
    string forst;
    int num;
    string::size_type begin = 0;

    message = lower_and_strip(message);

    for (char val : message) {
        num = return_encoded_char(key, begin, val);
        forst = to_string(num);
        code += forst; //*******************************
    }

    return code; 
}