list iterators incompatible error

Hello, I'm new to iterators in C++ and I'm facing problem with incompatible iterators. I've spent on this over 7 hours and still I can't figure out why it's not compatible or what I did wrong. I'm sure, I messed up iterator's operators or something close to it. Could you please help me?

Iterator header
1
2
3
4
5
6
7
8
9
10
11
    class iterator : public std::iterator < std::input_iterator_tag, Agent > {
    	list<Agent> agentList;
    	list<Agent>::iterator it;
    public:
    	iterator(list<Agent>** env, int rows, int cols);
    	void operator ++(int);
    	bool operator ==(const iterator& a) const;
    	bool operator !=(iterator& a) const;
    	Agent& operator*();
        void setEnd();
    };

Iterator implementation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
    Prostredi::iterator::iterator(list<Agent>** env, int rows, int cols) {
    	for (int r = 0; r < rows; r++) {
    		for (int c = 0; c < cols; c++) {
    			for (list<Agent>::iterator it = env[r][c].begin(); it != env[r][c].end(); it++) {
    				agentList.push_front(*it);
    			}
    		}
    	}
    	it = agentList.begin();
    }
    
    void Prostredi::iterator::operator++(int) {
        iterator copy(*this);
    	it++;
        return copy;
    }
    
    Agent& Prostredi::iterator::operator*() {
    	return *it;
    }
    
    bool Prostredi::iterator::operator== (Prostredi::iterator& a) const {
    	return !(it != a.it);
    }
    
    bool Prostredi::iterator::operator!= (Prostredi::iterator& a) const {
    	return a.it != it; // Here it crashes
    }

    void Prostredi::iterator::setEnd() {
	it = agentList.end();
      }

I'm calling it in this for-cycle. It crashes before first iteration while trying to compare it != p.dejEntityEnd().
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    void Stav::napln(const Prostredi & p) {
    	for (auto it = p.dejEntityBegin(); it != p.dejEntityEnd(); it++) {
    		agenti.push_back((*it).vypocitejNovyStav(p));
    	}
    }

    Prostredi::iterator Prostredi::dejEntityBegin() const {
    	return Prostredi::iterator(env, rows, cols);
    }

    Prostredi::iterator Prostredi::dejEntityEnd() const {
    	Prostredi::iterator it = Prostredi::iterator(env, rows, cols);
    	setEnd();
    	return it;
    }

Project has several classes and header files and everything is essential for correct setup, so it's hard to abstract the problem without leaving important info. If you need more information, I'll gladly reply or you can get my full project here (breakpoints are already set): https://www.dropbox.com/sh/9lsle7seiqz2wgj/AABJAXRXo350AhVmm-OqRSOqa
Error: https://i.stack.imgur.com/tNCXT.png
Last edited on
1
2
3
4
5
    void Prostredi::iterator::operator++(int) {
        iterator copy(*this);
    	it++;
        return copy;
    }


If your compiler doesn't complain about this, it might be time to get a new compiler.

Your iterator does not properly define a copy constructor or copy assignment operator, both of which will be required if you wish to to continue with the queer notion that an iterator should own its list.



Topic archived. No new replies allowed.