简体   繁体   中英

Reference to non-static member function must be called(vector)

i have a class which named class nameAndLastname and it has a

class nameAndLastname
{
private:
    vector<string> Names_Lastname();


public:

    void get();
    void Delete();
    string search();
};             

private:vector<string> Names_Lastnames(); and at first i got some names from another function and put them in vector<string> Names_Lastnames()

    void nameAndLastname::get()
{
    int SizeOFNames;
    cout<<"enter number of the names and last names";
    cin>>SizeOFNames;
    vector<string> Names_Lastnames(SizeOFNames);
    ifstream inFile;
    ofstream outFile;
    string fileName,Line;
    cout<<"whats the file name?:";
    cin>>fileName;
    inFile.open(fileName);
    getline(inFile,Line);
    cout<<"the first line of the file is:"<<endl;
    cout<<Line<<endl;
    cout<<"outputfilename?"<<endl;
    cin>>fileName;
    outFile.open(fileName);
    outFile<<Line<<endl;
    cout<<"now enter the names and last names";
    for (int i=0; i<=SizeOFNames; i++) {
        getline(cin,Names_Lastnames[i]);
        outFile<<Names_Lastnames[i]<<endl;
    }
    inFile.close();
    outFile.close();





}

and now i want to delete one of the names that user want to delete and i write this

void nameAndLastname::Delete(){
    string rname;
    cin>>rname;
    auto itr = find(Names_Lastnames.begin(), Names_Lastnames.end(), rname);
    if (itr != Names_Lastnames.end()) Names_Lastnames.erase(itr);
//error~>Reference to non-static member function must be called; did you mean to call it with no arguments?
//Use of undeclared identifier 'Names_Lastnames'
}

but i have this error "Reference to non-static member function must be called" .
i want to know how can i access to my vector from class named class nameAndLastname with reference

You have declared Names_Lastnames as a function that takes no parameters and returns a vector<string> .
(You did not put anything into it - if it looks like you did, you put your names into a vector with the same name.)

Remove the parentheses to make it a vector<string> .

Like this

class nameAndLastname
{
private:
    vector<string> Names_Lastname; // <--- no ()


public:

    void get();
    void Delete();
    string search();
};

void nameAndLastname::get()
{
    int SizeOFNames;
    cout<<"enter number of the names and last names";
    cin>>SizeOFNames;
    Names_Lastname.resize(SizeOFNames); // <--- resize class vector
    ...
}

void nameAndLastname::Delete(){
    string rname;
    cin>>rname;
    auto itr = find(Names_Lastname.begin(), Names_Lastname.end(), rname);
    if (itr != Names_Lastname.end()) 
        Names_Lastname.erase(itr);
}

As is common for newbies you had multiple mistakes and misunderstandings ganging up on you.

This version declares the vector (correctly without () ) in the class where all methods can access it, and resizes that vector (instead of redeclaring it) in the get method

void nameAndLastname::get()
{
//...
vector<string> Names_Lastnames(SizeOFNames);
//..
} // the scope of Names_Lastnames ends here. 

It seems like, you are referring a member variable with a similar name:Names_Lastname. If yes, correct it. Then if Names_Lastname is the variable, it is declared like a function in the class body. The parenthesis is not required. Hope you will try and repost the question if required.

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