简体   繁体   中英

error: reference to non-static member function must be called

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

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

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.

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