0

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.

7
  • 2
    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) Commented Jan 17, 2014 at 17:06
  • That part is working for me as I am reading through a file grabbing values. It's the map.insert part that I am having trouble with Commented Jan 17, 2014 at 17:08
  • 1
    use std::make_pair(name, myobj(num1,num2)); Also, you only need myobj as you have already initialized it Commented Jan 17, 2014 at 17:08
  • testcase Commented Jan 17, 2014 at 17:08
  • 2
    A simple typo: myobj(num1,num2) is nonsense, and should presumably just be myobj. Commented Jan 17, 2014 at 17:09

1 Answer 1

4
myobj(num1, num2)

This attempts to call your myobj object like a functor. Instead you just want to pass myobj:

mymap.insert(pair<string,ap_pair>(name, myobj));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.