Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ iterator for vector of struct Compiler Error

I created a struct type which contains two variables. I use this data type within a vector, that is again stored in a map, as following:

struct A {
    int x;
    Y y;
    A() {};
    A(int _x, Y _y) { x=_x, y=_y; };
};

typedef std::vector<A> LA;
typedef std::map<uint,LA> MB;

MB b;

When I try to use an iterator, such as

std::vector<LA>::iterator it = b[x].begin();

the compiler gives this error:

error: no viable conversion from '__wrap_iter< A *>' to '__wrap_iter< std::__1::vector < A, std::__1::allocator< A> > *>' std::vector< LA>::iterator it = b[x].begin();

candidate constructor (the implicit copy constructor) not viable: no known conversion from 'iterator' (aka '__wrap_iter< pointer>') to 'const std::__1::__wrap_iter< std::__1::vector< A, > std::__1::allocator< A> > *> &' for 1st argument class__wrap_iter

candidate constructor (the implicit move constructor) not viable: no known conversion from 'iterator' (aka > '__wrap_iter< pointer>') to 'std::__1::__wrap_iter< std::__1::vector< A, > std::__1::allocator< A> > *> &&' for 1st argument class__wrap_iter

candidate template ignored: disabled by 'enable_if'

What is the reason for this error? Although I tried several things like operator overloading, I could not solve this. Any help is appreciated.

like image 912
LikeMusic Avatar asked Aug 25 '15 17:08

LikeMusic


1 Answers

You don't have a vector of LA; you have a vector of A. So:

std::vector<A>::iterator it = b[x].begin();

or:

LA::iterator it = b[x].begin();

Looks like a simple typo to me?

(Not very surprising given these terrible type names...)

like image 121
Lightness Races in Orbit Avatar answered Oct 04 '22 20:10

Lightness Races in Orbit