-4
void Add(vector< vector<string> > &name,
         vector< vector<string> > &author,
         vector< vector<string> > &pub,
         int &total_books)
{
    Line();
    string book_name,book_author,book_pub;

    cout << "Please enter the book name: ";
    cin >> book_name;
    cout << "Please enter the author name: ";
    cin >> book_author;
    cout << "Please enter the publisher name: ";
    cin >> book_pub;

    name.push_back(book_name);
    author.push_back(book_author);
    pub.push_back(book_pub);

    ++total_books;

    cout << "The book has been successfully added.";
}

Compiler says this:

[Error] no matching function for call to
 'std::vector<std::vector<std::basic_string<char> >>::push_back(std::string&)'**

Does anybody know what's the problem?

JeJo
  • 30,635
  • 6
  • 49
  • 88
KYUNGWAN RYOO
  • 11
  • 1
  • 2
  • Line() is another fuinction to print out division line;; – KYUNGWAN RYOO Aug 20 '18 at 13:33
  • 2
    What's unclear about the error? You are trying to `push_back` a `string` into a `vector` holding `vector`. – Algirdas Preidžius Aug 20 '18 at 13:33
  • 7
    why is `name` a `vector >` instead of a `std::vector` ? – 463035818_is_not_an_ai Aug 20 '18 at 13:34
  • 1
    I think the downvotes here are a little harsh. The code and error message are in good format; and the issue perfectly clear. While it might be a fairly trivial question, I wouldn't go as far to say that it's "not helpful". Compiler error messages can certainly be cryptic; especially with templates; and for non-native English speakers I can only imagine the pain. – UKMonkey Aug 20 '18 at 14:12

3 Answers3

4

With a std::vector<std::vector<string> >, you need to push_back an instance of std::vector<string>.

Example:

std::vector<string> many_strings;
std::vector<std::vector<string> > matrix_of_strings;

many_strings.push_back("Sid");
many_strings.push_back("Alice");
many_strings.push_back("Bob");

matrix_of_strings.push_back(many_strings);

In your case, a better solution is to make a structure with the fields rather than using parallel vectors.

struct Book
{
    std::string title;
    std::string author;
    std::string publisher;
};

You can then add an input function:

struct Book
{
     //...
     void input_from_user();
};
void Book::input_from_user()
{
    std::cout << "Enter book title: ";
    std::getline(title);
    std::cout << "Enter book author: ";
    std::getline(author);
    std::cout << "Enter book publisher: ";
    std::getinle(publisher);
}

Your input process could look like this:

Book b;
b.input_from_user();

Your database would look like this:

std::vector<Book> database;
database.push_back(b);
Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
0

Your variables are vectors of vectors (vector<vector<string> > &name), which seems to be one level to deep - you cannot push_back a string into those, but only a vector of strings.

What are you trying to accomplish with this? Why not simply use vector<string> &name, which should work just fine?

Aganju
  • 6,295
  • 1
  • 12
  • 23
0

Sure, you're trying to push a string into a vector of vectors.

First, why are those vectors two-storeyed? If you change them into vector<string> no other changes will be needed (in this fragment.)

void Add(vector<string> &name, vector<string> &author, vector<string> &pub);

Next, if, due to some reason you still need them to be double level, well, you can insert fresh strings as singletons:

name.push_back({book_name});

(may need more verbosity in older standard:

author.push_back(vector<string>(1, book_author));

or add them to vector's most recent element:

pub.back().push_back(book_pub);

Perhaps, in the latter case you first need to check if the large vector is not empty, so there's a back().

It is unclear from your snippet what you're trying to achieve.

bipll
  • 11,747
  • 1
  • 18
  • 32