【发布时间】:2009-07-08 01:29:03
【问题描述】:
我怎样才能让它编译?错误是当我开始使用 boost::ref() 时。我认为 boost::ref 用于传递对 C++ 算法类的引用?
list<Object> lst;
lst.push_back(Object(1,2.0f));
lst.push_back(Object(3,4.3f));
struct between_1_and_10
{
int d;
void operator() (Object& value)
{
value.a += 5; value.b -= 3.3f;
cout << d << endl;
d += value.a;
}
};
between_1_and_10 val;
val.d = 4;
for_each(lst.begin(), lst.end(), boost::ref(val)); // Problem is here
printf("rg");
编辑这是人们建议的编译器错误:
1>c:\program files (x86)\microsoft visual studio 9.0\vc\include\algorithm(29) : error C2064: term does not evaluate to a function taking 1 arguments
1> c:\users\swangrun\desktop\minescout work\feat-000-gettargetimages\minescouttest\maintest.cpp(102) : see reference to function template instantiation '_Fn1 std::for_each<std::list<_Ty>::_Iterator<_Secure_validation>,boost::reference_wrapper<T>>(_InIt,_InIt,_Fn1)' being compiled
1> with
1> [
1> _Fn1=boost::reference_wrapper<main::between_1_and_10>,
1> _Ty=Object,
1> _Secure_validation=true,
1> T=main::between_1_and_10,
1> _InIt=std::list<Object>::_Iterator<true>
1> ]
【问题讨论】:
-
好的。它应该在 for_each() 行。但是有什么问题。您要么必须向我们提供错误或足够的代码,以便我们可以重现错误。
-
问题是它不能编译。我已接受答案,感谢您的参与!
-
Martin 的意思是“复制错误消息的文本并将其粘贴到您的问题中”。这样,我们可以向您解释编译器在说什么。它可能为您提供了查找错误所需的所有信息。至少,您应该这样做,以便搜索编译器错误文本的其他人可以找到问题及其答案。
-
谢谢!很抱歉误解了您的意图。