0

I have a problem(compiling problem) in my code. I would be happy if you can explain to me why this is happening and how I can fix it.

the error -

error C2512: 'Item': no appropriate default contractor available

*Item is a set

The line where this error happened not in my code, the problem in file called tuple but in my code the problem shows when I do this -

void init(map <int, Item>& itemli)
{
   Item temp("ro","1",1,1.99);
   itemli[1] = (temp);
   temp.setName("bo");
   temp.setSerialNumber("2");
   temp.setUnitPrice(2.22)
   // And so it goes on ...
}

My contractor -

Item::Item(string name,string serialNumber,int count, double unitPrice)
{
   _name = name;
   _serialNumber = serialNumber;
   _count = count;
   _unitPrice = unitPrice;
}

Thanks for the help !

Alon
  • 27
  • 4
  • The overloaded `operator[]` for a map is attempting to insert a new object, *then* copy your object via assignment to the returned reference. Without a default constructor, that isn't going to happen. Use `itemli.insert(std::make_pair(1, temp))`, but you should do it *after* you set all those properties. Either that or use the returns pair's iterator to invoke the target object's members after insertion. – WhozCraig Jan 17 '15 at 07:18
  • @WhozCraig Where am I supposed to get it? Instead what I do ? – Alon Jan 17 '15 at 07:20

1 Answers1

0

You are misusing the map in this line itemli[1] = (temp);

Instead you should do

Item temp("ro","1",1,1.99);
temp.setName("bo");
temp.setSerialNumber("2");
temp.setUnitPrice(2.22)
itemli.insert(std::make_pair(1, temp);//Moved so that setName will relect to temp in Map
// And so it goes on ...

The temp is an normal object (Not Pointer) so if you are inserting into map the std::map will make a copy of your Item and put it into itemli

Sridhar DD
  • 1,972
  • 1
  • 10
  • 17