1

I'm working on a program that collects family last names and then asks for names of first name of family members. I'm using an istringstream object to separate the names collected from a getline. For the step where I try to fill the elements in the map, I get the following error. I know that you can create keys by supplying them in brackets following the name of the map. If that method is valid, why do I get the following error.

ex11_14.cpp:19:6: error: reference to non-static member function must be called
        family[lname].push_back[fname];

Code:

#include <iostream>
#include <map>
#include <vector>
#include <sstream>
int main()
{
    std::string lname, fname;
    std::string last, children;
    std::map<std::string, std::vector<std::string>> family;
    std::cout << "Enter family names" << std::endl;
    getline(std::cin, last);
    std::istringstream i{last};
    std::cout << "Enter children's names" << std::endl;
    while(i >> lname) {
        std::cout << lname << std::endl;
        getline(std::cin, children);
        std::istringstream j{children};
        while(j >> fname)
            family[lname].push_back[fname];
    }
    for(auto &c:family) {
        std::cout << c.first << std::endl;
        for(auto &w:c.second)
            std::cout << w << " ";
        std::cout << std::endl;
    }
    std::cout << std::endl;
    return 0;
}

2 Answers 2

3

Sorry. push_back should be called with () not []. Fixes the issue.

Sign up to request clarification or add additional context in comments.

Comments

0

It seems, this line:

family[lname].push_back[fname];

wants to read

family[lname].push_back(fname);

BTW, don't use std::endl. Use '\n' to get a newline and std::flush to flush the stream: use of std::endl is most often a performance problem without being useful in the first place.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.