Weird errors with std::map

Apprentice
Posts: 19
Joined: 2005.11
Post: #1
Hi. I'm using a std::map to store font glyphs so that I can look them up by character. (Can't simply use an array since I use unicode characters.)

Anyway, when I try to do the following:

Code:
GlyphMapConstItr itr = m_glyphs.find(c);
GlyphMetrics& m = (m_glyphs.end() == itr) ? m_unkownGlyph : *itr;

GCC gives me this long and incomprehensible STL error message:

Quote:/Path/to/File.cpp:164: error: no match for ternary 'operator?:' in '((const Qoph::Font*)this)->Qoph::Font::m_glyphs. std::map<_Key, _Tp, _Compare, _Alloc>::end [with _Key = wchar_t, _Tp = Qoph::Font::GlyphMetrics, _Compare = std::less<wchar_t>, _Alloc = std::allocator<std::pair<const wchar_t, Qoph::Font::GlyphMetrics> >]().std::_Rb_tree_const_iterator<_Tp>::operator== [with _Tp = std::pair<const wchar_t, Qoph::Font::GlyphMetrics>](((const std::_Rb_tree_const_iterator<std::pair<const wchar_t, Qoph::Font::GlyphMetrics> >&)((const std::_Rb_tree_const_iterator<std::pair<const wchar_t, Qoph::Font::GlyphMetrics> >*)(& itr)))) ? ((const Qoph::Font*)this)->Qoph::Font::m_unkownGlyph : itr. std::_Rb_tree_const_iterator<_Tp>::operator* [with _Tp = std::pair<const wchar_t, Qoph::Font::GlyphMetrics>]()'

It seems to me that the compiler is looking for a overloaded ternary operator (weird, because I didn't think the ternary operator was overloadable in C++), or that the result of "(m_glyphs.end() == itr)" isn't a bool, which it surely should be.

All the Glyph structures and typedefs follow.

Code:
struct GlyphMetrics {
    Vector2i position;
    Vector2i size;
    Vector2i origin;
    
    int advance;            
};

typedef std::map<wchar_t, GlyphMetrics> GlyphMap;
typedef GlyphMap::iterator GlyphMapItr;
typedef GlyphMap::const_iterator GlyphMapConstItr;

GlyphMap m_glyphs;
GlyphMetrics m_unkownGlyph;

If anyone can see the problem here, please let me know.
Quote this message in a reply
Moderator
Posts: 1,140
Joined: 2005.07
Post: #2
Is GlyphMapConstItr your own class/subclass, or is it just a typedeffed Map<types>::iterator? If the former, then you may have to make sure that you overload operator ==, and make sure that it takes Map<types>::iterator. If the latter, then I don't know, since it should already have operator == overloaded correctly. (I'm pretty sure the operator == is somehow the root of your problem, though)
Quote this message in a reply
Apprentice
Posts: 19
Joined: 2005.11
Post: #3
GlyphMapConstItr is defined as:
Code:
typedef std::map<wchar_t, GlyphMetrics> GlyphMap;
typedef GlyphMap::const_iterator GlyphMapConstItr;
Quote this message in a reply
DoG
Moderator
Posts: 869
Joined: 2003.01
Post: #4
Tey putting parenthesis like this:
foo=(a?b:c);
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #5
It's a const iterator, therefore you can't modify what it points to. I think this should work:

Code:
const GlyphMetrics& m = (m_glyphs.end() == itr) ? m_unkownGlyph : *itr;
Quote this message in a reply
Apprentice
Posts: 19
Joined: 2005.11
Post: #6
Thanks OneSadCookie. Tha was one of the problems. The other was that
... : *itr;
should have been
... : (*itr).second;
since a std::map iterator points to a std :: pair.

Sometimes I feel really stupid. although, in my defense, the error message wasn't the world's most helpful.

Thank you all for the help.
Quote this message in a reply
DoG
Moderator
Posts: 869
Joined: 2003.01
Post: #7
Just for the record, using the form itr->second instead of (*itr).second seems cleaner.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #8
I'm not sure iterators are guaranteed to provide ->, where they are guaranteed to provide *... but I can't remember why I might think this Smile
Quote this message in a reply
DoG
Moderator
Posts: 869
Joined: 2003.01
Post: #9
OneSadCookie Wrote:I'm not sure iterators are guaranteed to provide ->, where they are guaranteed to provide *... but I can't remember why I might think this Smile
I have no idea why you think that, iirc both * and -> are valid for dereferencing an iterator, and I don't recall reading -> was optional: http://www.sgi.com/tech/stl/trivial.html
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  weird compiler warnings/errors in Xcode 2.0 Andrew 15 6,360 May 13, 2005 01:07 AM
Last Post: Andrew