I am trying to insert an object into a map. You can ignore most of the code here, but I'll include it to help. It's the line mymap.insert(pair(name, myobj(num1, num2))); That is giving me the error.
struct ap_pair {
ap_pair(float tp, float tm) : total_price(tp), total_amount(tm) {};
ap_pair & operator+=(const ap_pair &);
float total_price;
float total_amount;
};
void APC :: compute_total ()
{
string name;
map<string, ap_pair> :: iterator my_it;
float num1, num2, num3;
while (!fs.eof() )
{
fs >> name >> num1 >> num2; //read in file
ap_pair myobj(num1, num2); //send the weight/count and per unit price ap_pair
my_it = mymap.find(name); //returns iterator
if (fs.eof()) break; //makes it so the last line is not repeated
mymap.insert(pair<string,ap_pair>(name, myobj(num1, num2))); //ERROR IS HERE
num3= num1*num2;
total_amount+=num1;
total_price+= num3;
}
}
I am getting an error when compiling saying " error: no match for call to '(ap_pair) (float&, float&)". Why is that? What is wrong with doing what I did? I've been working on this for over an hour with no solution in sight. Any ideas? I can give some more ideas of what I am trying to do if needed. I think this might be a simple syntax issue I am looking over though.
while (!fs.eof() )Where did you learn to do this? It's wrong, in your code (though you do compensate for it, it's sub-optimal)std::make_pair(name, myobj(num1,num2));Also, you only needmyobjas you have already initialized itmyobj(num1,num2)is nonsense, and should presumably just bemyobj.