The thing is, I am not (knowing trying to use any default constructor of beatle::beatle
the error:
1> ecosystem.cpp
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\map(172): error C2512: 'beatle::beatle' : no appropriate default constructor available
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\map(165) : while compiling class template member function 'beatle &std::map<_Kty,_Ty>::operator [](int &&)'
1> with
1> [
1> _Kty=tokenID,
1> _Ty=beatle
1> ]
1> c:\users\zak\documents\visual studio 2010\projects\ascii_sivvure\ascii_sivvure\ecosystem.h(22) : see reference to class template instantiation 'std::map<_Kty,_Ty>' being compiled
1> with
1> [
1> _Kty=tokenID,
1> _Ty=beatle
1> ]
Code with non relevant things stripped:
header:
typedef std::map<tokenID,beatle> Beatles;
class ecosystem
{
private:
line 22: Beatles m_Beatles;
};
source:
ecosystem::ecosystem(): m_output( output() )
{
Beatles m_Beatles;
}
void ecosystem::populate()
{
if (m_isMatingSeason && ( random(0,1000) < rateMATING ) )
{
beatle babyBeatle = breed();
m_Beatles[babyBeatle.getTokenID()] = babyBeatle;
m_field.occupy(babyBeatle.getTokenID(), babyBeatle.getLocation() );
}
}
I’ve been trying for hours using different combinations of trying to properly define/declare the maps. At one point intellisense starting saying it wanted pointers to object here:
m_Beatles[babyBeatle.getTokenID()] = babyBeatle;
and that led me down a sad path.
This is all happening after my first(and hopefully last) refactoring binge, its been over a week since I’ve been able to compile… I probably have 40 hours just trying to get it working again.
std::map<>::operator[]requires a default constructor. This is because it first creates an entry in the map then does theoperator=that you are calling.If you absolutely want to use an
std::mapbut do not want to provide a default constructor (perhaps it is illogical for your case?) you can use:std::map<>::insert()To explicitly insert the object into the code. This makes things a bit more complicated then because look ups must also use find.
I just noticed that Adam Rosenfield already posted this info in a comment but going ahead and leaving this as a separate answer.