ive been trying to improve my skills when it comes on to using maps and iterators but i seem to be mostly failing can someone please tell me where im going wrong or point me in the right direction

compiler error

a.cpp:20:55: error: conversion from ‘std::map<std::basic_string<char>, Object*>::iterator {aka std::_Rb_tree_iterator<std::pair<const std::basic_string<char>, Object*> >}’ to non-scalar type ‘std::map<std::basic_string<char>, Object>::iterator {aka std::_Rb_tree_iterator<std::pair<const std::basic_string<char>, Object> >}’ requested
a.cpp:20:71: error: no match for ‘operator<’ in ‘itr < _c.std::map<_Key, _Tp, _Compare, _Alloc>::end<std::basic_string<char>, Object*, std::less<std::basic_string<char> >, std::allocator<std::pair<const std::basic_string<char>, Object*> > >()’

my code

#include "object.hpp"
#include <map>

int main()

    std::map<string, Object*> _c;

    Object *a = new Object();
    Object *b = new Object();
    Object *c = new Object();
    Object *d = new Object();

    _c["dbdb"] = a;
    _c["dbh"] = b;
    _c["dbdfgdb"] = c;
    _c["dbdbgdsfg"] = d;

    for( std::map<string, Object>::iterator itr=_c.begin(); itr < _c.end(); itr++)



Edited by glenwill101

4 Years
Discussion Span
Last Post by glenwill101

You're trying to make an iterator that deals with maps like this: std::map<string, Object>
but your actual map looks like this:std::map<string, Object*>

As an aside, one of the very useful things brought to us by C++11 is the auto keyword allowing you to make iterators something like this:
auto itr=_c.begin();

Edited by Moschops


You just forgot the * next to the type Object in your for-loop. It should be:

for( std::map<string, Object* >::iterator itr=_c.begin(); itr < _c.end(); itr++)

You first error is solved by answers before this. You second error is "itr < _c.end()"... Use "itr != _c.end()" instead

According to this, you can only compare iterators with "==" or "!=".

Edited by kvahanyan


just realized i never thanked you guys for helping me guess this can now be markied as solved

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.