[英]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)));
这是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&)". 我在编译时说“错误:调用'(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.
我认为这可能是我正在寻找的简单语法问题。
myobj(num1, num2)
This attempts to call your myobj
object like a functor. 这会尝试像函子一样调用
myobj
对象。 Instead you just want to pass myobj
: 相反,您只想传递
myobj
:
mymap.insert(pair<string,ap_pair>(name, myobj));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.