Mdjon26
Mdjon26

Reputation: 2255

How to use pair correctly in C++

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.

Upvotes: 0

Views: 112

Answers (1)

Joseph Mansfield
Joseph Mansfield

Reputation: 110658

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));

Upvotes: 4

Related Questions