简体   繁体   中英

C++ Vector issue “qualifiers that prevent match”

void Exam:: read_questions(string filename) const{
    ifstream file;
    file.open(filename);
    if (file.is_open()){
        string line;
        while(getline(file,line)){
            Question* currentQuestion =  parse_question(line);
            question_list.push_back(currentQuestion);
        }
    }else{
        cout << "invalid file" << endl;
    }
    file.close();
}

Having an issue with the line question_list.push_back(currentQuestion); it gives an error saying

no instance of overloaded function

and also says

push_back' : 2 overloads have no legal conversion for 'this' pointer 1 with [ _Ty=Question * ]

What does this error mean and how do I fix it?

Here is the header file to Exam:

class Exam 
{
public:
    Exam();
    Exam(int num_q, int min_chap, int max_chap);
    void read_questions(string filename) const;
    ~Exam();
    void write_exam(string filename) const;
    void write_key(string filename) const;
    void shuffle();

private:
    vector<Question *> question_list;
    int minC;
    int maxC;
    int numQ;
};

The problem is that you declare read_question this way:

void Exam:: read_questions(string filename) const

The const here is saying that your function will not modify the object you call it on. Then in the function, you do this:

question_list.push_back(currentQuestion);

question_list is a member variable, so you broke your promise. The compiler is calling you on it.

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