简体   繁体   中英

C++ iterator on a list error

I have in a C++ class the following : I want to use an iterator to have an element of the questions list at a time by calling the getNextQuestion() function after checking if the iterator is still valid by calling isValid(). It gives me the following terrible error :

passing ‘const iterator {aka const std::_List_iterator<domain::Question>}’ as ‘this’ argument of ‘std::_List_iterator<_Tp>::_Self& 
std::_List_iterator<_Tp>::operator++() [with _Tp = domain::Question,std::_List_iterator<_Tp>::_Self = 
std::_List_iterator<domain::Question>]’ discards qualifiers [-fpermissive]

#ifndef TESTREPOSITORY_H_
#define TESTREPOSITORY_H_

#include <iostream>
#include <iterator>
#include <list>
#include <algorithm>
#include <fstream>
#include "../domain/question.h"

using namespace domain;

namespace repository{
template<class T>
class TestRepository{
std::string file;
std::list<T> questions;
typename std::list<T>::iterator it;
public:
TestRepository(std::string& file=""):file(file){
    this->questions = this->getQ();
    this->it = this->questions.begin();
};

std::list<T> getQ() const{
    std::list<T> listq;
    using namespace std;
    string line;
    std::ifstream fin(file.c_str());
    while(fin.good()){
        Question q;
        fin >> q;
        listq.push_back(q);
    }
    fin.close();
    return listq;
}

const bool isValid() const{
    return this->it != this->questions.end();
}

const T getNextQuestion() const{
    T q = (*this->it);
    ++this->it;
    return q;
}

};
}

#endif /* TESTREPOSITORY_H_ */

Here is the code where I call these funcitons,maybe here is the problem coming from :

#include "TestController.h"
#include "../domain/test.h"
#include <iostream>
#include <list>
#include <iterator>

 namespace controller{

TestController::TestController(repository::TestRepository<domain::Question>* repo,int   testId){
this->repo = repo;
this->testId = 0;
}

const test TestController::getCurrentTest() const{
test test(this->testId,0,0);
return test;
}

const bool TestController::isValid() const{
return this->repo->isValid();
 }

const Question TestController::getNextQuestion() const{
return this->repo->getNextQuestion();
}



}

Here:

const T getNextQuestion() const{
    T q = (*this->it);
    ++this->it;
    return q;
}

You are changing the field it and you're not supposed to, because the method is const . If you want to use const only in the meaning that your "repository" is not modified, but its internal iterator is irrelevant you can use the mutable keyword:

mutable typename std::list<T>::iterator it;

You are trying to modify a member in a const member function, which is forbidden (thats the point of const member functions) :

const T getNextQuestion() const{
    T q = (*this->it);
    ++this->it;   // << Here
    return q;
}

This method should be non const, or consider having your member iterator be mutable :

mutable typename std::list<T>::iterator 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