This is my CreateCustomer method:
CustomerData Database::createCustomer(std::string const& name) {
CustomerData data(customersDb.size()+1, name);
customersDb[data.number()] = data;
// customersDb.insert(std::pair<uint, CustomerData>(data.number(), data));
return data;
}
Constructor of class CustomerData:
CustomerData::CustomerData(uint number, std::string const& name) {
m_number = number;
m_name = name;
}
Incorrect line is this one:
customersDb[data.number()] = data;
Thing is, the next line works. I can’t figure it out and can’t use insert because accessing element with [] brackets doesn’t work either.
I’ve tried even creating copy constructor but with no effect:
CustomerData::CustomerData(CustomerData &data) {
m_number = data.number();
m_name = data.name();
}
These are the errors:
Database.cpp:5: instantiated from here
stl_map.h:450: error: no matching function for call to 'CustomerData::CustomerData()'
CustomerData.h:18: candidates are: CustomerData::CustomerData(CustomerData&)
CustomerData.h:17: note: CustomerData::CustomerData(uint, const std::string&)
I use Mingw.
operator[]with the standard map template default-constructs an element with the given key if none already exists.Your element type has no default constructor, so this cannot happen.
You’ll have to use
.insertinstead.To modify an existing element, retrieve an iterator with
.find, dereference and do your thing.