Angus said:
I am writing a class as a wrapper around a std::map.
The class is:
template<class TKey, class TValue>
class CGeneralMap : protected map<TKey, TValue>
At the moment I have basic functions like Add, Remove etc but I am
really struggling with GetValue, which will be like the value obtained
from [key]
Eg if I create a:
CGeneralMap<int, std::string> TestMap;
and I have a key 3 with std::string value of John. then how in my
GetValue function do I extract the "John"?
In my GetValue function I tried:
You could of course use
value = (*this)[3];
But I think the "optimal" solution will be
value = operator[](3);
Both of those call the "operator[]" of the std::map, which is what you want.
Cheers,
Daniel
--
Got two Dear-Daniel-Instant Messages
by MSN, associate ICQ with stress--so
please use good, old E-MAIL!- Hide quoted text -
- Show quoted text -
Ah brilliant, that is what I needed. But now I have a conversion
problem.
If on line 366 I have:
Value = operator[](Key);
and on line 367 I have:
Value = (*this)[Key];
Then I get these compilation errors in Microsoft Visual C++ version 6:
TestBed.cpp
generalmap.h(366) : error C2662: '[]' : cannot convert 'this' pointer
from
'const class CGeneralMap<int,class std::basic_string<char,struct
std::char_traits<char>,class std::allocator<char> > >'
to
'class std::map<int,class std::basic_string<char,struct
std::char_traits<char>,class std::allocator<char> >,struct
std::less<int>,class std::allocator<class
std::basic_string<char,struct std::char_traits<char>,class
std::allocator<char> > > > &'
Conversion loses qualifiers
c:\program files\microsoft visual studio\vc98\include
\xmemory(59) : while compiling class-template member function 'void
__thiscall CGeneralMap<int,class std::basic_string<char,struct
std::char_traits<char>,class std::allocator<char> > >::G
etValue(const int,class std::basic_string<char,struct
std::char_traits<char>,class std::allocator<char> > &) const'
generalmap.h(367) : error C2678: binary '[' : no operator defined
which takes a left-hand operand of type 'const class
CGeneralMap<int,class std::basic_string<char,struct std::char_traits<
char>,class std::allocator<char> > >' (or there is no acceptable
conversion)
c:\program files\microsoft visual studio\vc98\include
\xmemory(59) : while compiling class-template member function 'void
__thiscall CGeneralMap<int,class std::basic_string<char,struct
std::char_traits<char>,class std::allocator<char> > >::G
etValue(const int,class std::basic_string<char,struct
std::char_traits<char>,class std::allocator<char> > &) const'
ie I am trying both options.