vendredi 1 novembre 2019

conversion to non-scalar type in an iteration over a vector inside a set

I have a set of vectors of pointer. I try to iterate over a selected vector from the set.

The code does not compile as there is a conversion from * const* to non-scalar type **.

error: conversion from ‘__normal_iterator<Foo* const*,[...]>’ to non-scalar type ‘__normal_iterator<Foo**,[...]>’ requested

I read that this is because since c++11 set has no non-constant iterators (C++ Error: Conversion to Non-Scalar Type). But I don't know how to cast it so that it would work.

A minimal code which shows the error

#include <set>
#include <vector>

using namespace std;

class Foo {
  int v;
};

int main() {
  set<vector<Foo *>> setVecF;
  vector<Foo *> vecF;
  Foo * f = new Foo;

  vecF.push_back(f);
  setVecF.insert(vecF);

  set<vector<Foo *>>::iterator sit(setVecF.begin());
// the line below does not compile  
//vector<Foo *>::iterator vit = sit->begin();
  return 0;
}

I expect that vit would point at the beginning of the selected vector inside the set, so that I could iterate over it. But it does not as I cannot use ->begin() on sit. Any ideas on how to do it?

Aucun commentaire:

Enregistrer un commentaire