简体   繁体   中英

Error populating a vector of vectors: no matching function to call for std::basic_string::push_back

I'm trying to do Accelerated C++'s example 5.1 in which I am supposed to create a permuted index. So I am trying to put each rotation (which come out fine as I tested with the cout statements first) into a vector of itself.

#include <cctype>
#include <iostream>
#include <string>
#include <vector>

#include "split.h"


using namespace std;


void rotate(vector<string> lineSplit){

    // create vector of vectors, length is the amount
    // of words. Each vector will contain a rotation
//    std::vector<string> rotations(lineSplit.size(), "");
    std::vector<string> rotations(lineSplit.size(), "");

    for (int i=0; i<lineSplit.size(); i++){
        cout << "New Rotation: " << endl;

        for (int j=i; j<lineSplit.size(); j++){
            rotations[i].push_back(lineSplit[j]);
            cout << lineSplit[j] << endl;
        }
        // cout << "START IN HERE!" << endl;
            for (int j=0; j<i; j++){
                rotations[i].push_back(lineSplit[j]);
                cout << lineSplit[j] << endl;
            }
    }
}

int main()
{

    string line="The quick brown fox";
    vector<string> lineSplit = split(line);

    cout << "size of lineSplit is: " << lineSplit.size() << endl;

    rotate(lineSplit);

}

So rotations should be a vector of vectors, each one containing a rotation (as per the comment statements) I thought I would be able to add to each individual vector with "rotations[i].push_back(lineSplit[j])" but the compiler is complaining:

main.cpp: In function ‘void rotate(std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >)’:
main.cpp:23: error: no matching function for call to ‘std::basic_string<char, std::char_traits<char>, std::allocator<char> >::push_back(std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)’
/usr/include/c++/4.2.1/bits/basic_string.h:869: note: candidates are: void std::basic_string<_CharT, _Traits, _Alloc>::push_back(_CharT) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]
main.cpp:28: error: no matching function for call to ‘std::basic_string<char, std::char_traits<char>, std::allocator<char> >::push_back(std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)’
/usr/include/c++/4.2.1/bits/basic_string.h:869: note: candidates are: void std::basic_string<_CharT, _Traits, _Alloc>::push_back(_CharT) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]

Did I initialize rotations incorrectly?

 rotations[i].push_back(lineSplit[j]);

rotations is vector of string, so rotations[i] is string. lineSplit is vector of string, so lineString[i] is string. You can push_back only chars in string, not string in string. Look like, that rotations should have type vector<vector<string> > .

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