Bogdan M.
Bogdan M.

Reputation: 2181

Member function of a class isn't recognized in a function

I'm having a clss student and I used it's member functions in same header of the repository without problem... but now in this function I get an error:

..\StudentRepository.cpp:22:7: error: request for member 'setName' in 'st', which is of non-class type 'Student()'

And this is the function:

void StudentRepository::loadStudents(){
    ifstream fl;
    fl.open("studs.txt");
    Student st();
    string s,ss;
    int loc;
    if(fl.is_open()){
        while(!(fl.eof())){
            getline(fl,s);
            loc = s.find(",");
            ss = s.substr(0,loc);
            st.setName(ss);

        }
    }
    else{
        cout<<"~~~ File couldn't be open! ~~~"<<endl;
    }
    fl.close();

}

I have to mention that in the same file I do use them for example this function:

 void StudentRepository::editStudent(Student A){
    int i;
    i = findByName(A.getName());
    if( i != 0 || i != NULL){
        students[i].setGroup(A.getGroup());
        students[i].setId(A.getID());
    }
    else{
        throw RepoException("The name does not exist!");
    }
    saveStudents();
}

Upvotes: 1

Views: 112

Answers (2)

Alok Save
Alok Save

Reputation: 206526

 Student st();

should be:

 Student st;

Student st(); does not create an object st of the type Student it declares a function by the name st which takes no parameters and returns a Student object.

This is sometimes called Most Vexing Parse in C++.

Upvotes: 4

Ry-
Ry-

Reputation: 224913

Remove the parentheses from st's declaration.

Student st();

Upvotes: 4

Related Questions