简体   繁体   English

列表错误的C ++迭代器

[英]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(). 我在C ++类中具有以下内容:我想通过调用isValid()检查迭代器是否仍然有效之后,通过调用getNextQuestion()函数来一次使用迭代器来获取问题列表的元素。 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 . 您正在更改字段it并且不应该这样做,因为方法是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: 如果仅在未修改“存储库”的意义上使用const ,但其内部迭代器无关紧要,则可以使用mutable关键字:

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成员函数中的成员,这是被禁止的(那是const成员函数的要点):

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 : 此方法应为非const,或考虑使成员迭代器mutable

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM