x******a 发帖数: 6336 | 1 code和编译的错误在下面。
尝试了另外两种方式编译都没有问题:
1. 把struct Obj和double plus(..)定义在class外面
2. 在total()的定义里面,用for loop而不用std::accumulate.
请教怎么回事?谢谢!
#include
#include
#include
class Foo{
struct Obj{
double a;
explicit Obj(const double& a_): a(a_){}
};
double plus(double result, const Obj& obj){
return result+ obj.a;
}
std::vector v;
public:
void append(const double& a){ v.push_back(Obj(a));}
double total() const{
return std::accumulate(v.begin(), v.end(), 0.0, plus);
}
};
int main()
{
Foo foo;
for(int i=0; i<10; ++i){
foo.append(2*i);
}
std::cout<
return 0;
}
$ g++ test.cpp
test.cpp: In member function 'double Foo::total() const':
test.cpp:21:57: error: no matching function for call to 'accumulate(std::
vector::const_iterator, std::vector::const_iterator,
double, )'
test.cpp:21:57: note: candidates are:
In file included from /usr/include/c++/4.7/numeric:62:0,
from test.cpp:3:
/usr/include/c++/4.7/bits/stl_numeric.h:121:5: note: template
InputIterator, class _Tp> _Tp std::accumulate(_InputIterator, _InputIterator
, _Tp)
/usr/include/c++/4.7/bits/stl_numeric.h:121:5: note: template argument
deduction/substitution failed:
test.cpp:21:57: note: candidate expects 3 arguments, 4 provided
In file included from /usr/include/c++/4.7/numeric:62:0,
from test.cpp:3:
/usr/include/c++/4.7/bits/stl_numeric.h:147:5: note: _Tp std::accumulate(_
InputIterator, _InputIterator, _Tp, _BinaryOperation) [with _InputIterator =
__gnu_cxx::__normal_iterator >; _Tp
= double; _BinaryOperation = double (Foo::*)(double, const Foo::Obj&)]
/usr/include/c++/4.7/bits/stl_numeric.h:147:5: note: no known conversion
for argument 4 from '' to 'double (Foo:
:*)(double, const Foo::Obj&)' | t****t 发帖数: 6806 | 2 make Foo::plus static.
if plus is non-static, then you need an object to call it, such as
std::accumulate(v.begin(), v.end(), 0.0, std::function(&Foo::plus, this));
【在 x******a 的大作中提到】 : code和编译的错误在下面。 : 尝试了另外两种方式编译都没有问题: : 1. 把struct Obj和double plus(..)定义在class外面 : 2. 在total()的定义里面,用for loop而不用std::accumulate. : 请教怎么回事?谢谢! : #include : #include : #include : class Foo{ : struct Obj{
| x******a 发帖数: 6336 | 3 thanks thrust, it works now with static.
the one with std::function does not.
【在 t****t 的大作中提到】 : make Foo::plus static. : if plus is non-static, then you need an object to call it, such as : std::accumulate(v.begin(), v.end(), 0.0, std::function(&Foo::plus, this));
| t****t 发帖数: 6806 | 4 ehh, i just wrote it quick, of course it doesn't work that way, you have to
write
std::accumulate(v.begin(), v.end(), 0.0, std::bind(&Foo::plus, this, placeho
lder::_1, placeholder::_2));
and make Foo::plus a const function, too.
【在 x******a 的大作中提到】 : thanks thrust, it works now with static. : the one with std::function does not.
| x******a 发帖数: 6336 | 5 thanks,
请问直接用vector做Foo的data member
和
用point to Obj做data member
那个比较好?
to
placeho
【在 t****t 的大作中提到】 : ehh, i just wrote it quick, of course it doesn't work that way, you have to : write : std::accumulate(v.begin(), v.end(), 0.0, std::bind(&Foo::plus, this, placeho : lder::_1, placeholder::_2)); : and make Foo::plus a const function, too.
|
|