C++错误,编译器将无法识别字符串::p ush_back

C++ error, compiler won't recognize string::push_back

本文关键字:字符串 ush back 识别 错误 编译器 C++      更新时间:2023-10-16

它具有以下问题的功能:

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。

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