Reputation: 4995
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;
}
Upvotes: 1
Views: 6444
Reputation: 153830
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.
Upvotes: 0