Hi!
Finally got the source of your compiler error:
Don't include string.h but only string:
For MS-Copiler this makes a difference... (tested on MSVC++ 7.1.3088)
And te iterator in line 28 must not be const.
Reganding your other questions:
ad 1): The definitoin of Struct as an inner class to Assoc doesn't waste any space at runtime, because it is like a type definition. the struct Pair {...} doesn't generate executable code. btw, I don't see the reason fpr definig Pair on your own - std::pair<std::string, double> does basically the same.
ad 2) You have a const and a non-const version of the the [] operator. This is important if you use the [] operator inside a method which is defined const itself. In order to work your declaration shall be
const double& operator[] (const string&) const ;
and you have to implement it, of course. The compiler will then pick the const operator if the this -pointer ist const and the volatile version otherwise.
ad 3) Yes, definitely. this is obviously the non-const [] operator...
Try it out by placing this
1 2
|
std::string key("johndoe");
std::cout << (*this)[key] << std::endl;
|
in the printAll method while not having implemented the const-version of the [] operator - the compiler will complain!