I don't really understand why a class needs to have a default constructor when it is being used as a map value.
If you don't have a default constructor, it will give you an error saying: In template: no matching constructor for initialization
#include <iostream>
#include <map>
class MyClass
{
public:
MyClass(int test) {};
MyClass() {};
};
int main() {
std::map<int, MyClass> myMap;
myMap[1] = MyClass(42);
}
This code will give me no error since I have a default constructor. Can someone help me understand why I need a default constructor. Thanks.