error: conversion from '...iterator...' to non scalar type '...iterator...' requested

0

Chyba zaczynam się robić leniwy (Zmęczony extends Leniwy, Leniwy implements Stary), umieszczając problem na forum, zamiast samemu go poszukać. :(

Ale do rzeczy.

Wyskakuje mi taki błąd:
Graph.cpp:31:14: error: conversion from '__gnu_cxx::__normal_iterator<const meh_projekt_2::Vertex*, std::vector<meh_projekt_2::Vertex> >' to non-scalar type 'std::vector<meh_projekt_2::Vertex>::iterator {aka __gnu_cxx::__normal_iterator<meh_projekt_2::Vertex*, std::vector<meh_projekt_2::Vertex> >}' requested

Oto kawałek mojego kodu, w którym on występuje (btw. występuje w kilku podobnych metodach):

const Vertex & Graph::getVertex(int vertexNumber) const {
	vector<Vertex>::iterator it = find(this->g.begin(),
        this->g.end(),
        [vertexNumber](const Vertex & v) {
        	return v.getNumber() == vertexNumber;
        }); // W TEJ LINIJCE JEST BŁĄD
	...
}

Pole Graph::g:

private:
	vector<Vertex> g;

Metoda getNumber():

inline int Vertex::getNumber() const {
	return this->number;
}

Jak coś jeszcze będzie potrzebne, oczywiście wkleję.

4

Nie możesz w metodzie const utworzyć non-const iteratora na składową klasy.

0

@Satirev, po zmianie na const vector<Vertex>::iterator it:

Graph.cpp:31:14: error: conversion from '__gnu_cxx::__normal_iterator<const meh_projekt_2::Vertex*, std::vector<meh_projekt_2::Vertex> >' to non-scalar type 'const iterator {aka const __gnu_cxx::__normal_iterator<meh_projekt_2::Vertex*, std::vector<meh_projekt_2::Vertex> >}' requested

4

Patrząc na to co próbujesz zrobić to lepiej zrób map<unsigned,Vertex> lub lepiej unordered_map<unsigned,Vertex>

0

Rozwiązałem: właściwym problemem było: raz, że brak const_iteratora, a dodatkowo powinno być find_if zamiast find.

1 użytkowników online, w tym zalogowanych: 0, gości: 1