I’m getting the following error in my C++ program:
1> c:\users\thom\documents\cworkspace\barnaby\barnaby\timezone.cpp(14) : see reference to class template instantiation 'std::map<_Kty,_Ty>' being compiled
This is down a bit in the error stack but points to this line of code:
static std::map<const std::string, Timezone> timezoneMap;
The reason is that Timezone has a rather elaborate constructor, but no default constructor. Here’s that part of the error:
c:\program files\microsoft visual studio 10.0\vc\include\map(215): error C2512: 'Timezone::Timezone' : no appropriate default constructor available
1> c:\program files\microsoft visual studio 10.0\vc\include\map(210) : while compiling class template member function ‘Timezone &std::map<_Kty,_Ty>::operator [](const std::basic_string<_Elem,_Traits,_Ax> &)’
My question is, why? Why is the map trying to construct a Timezone object? Why should it need too if I always put fully formed objects into my map? Especially, why this error when I initialize the map?
You’re probably using the
map‘soperator[]which does require the default constructor (if it didn’t, how would it handle the case where the key doesn’t exist in the map?). If you useinsertinstead you may be able to get away with not providing one (I can’t recall if the standard requires a default constructor for all maps, or just when you use that operator).