由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 请教struct inside class的问题(C++)
相关主题
why use static function here?码工试题 (转载)
问个c++的template的问题有没有办法让一个类的变量只读,不是const?
请问关于overloading <<问个C++ 编译器临时变量的问题 (转载)
c++ does not check const for extern variable?What does the default constructor do?
const int foo()啥意思?写惯了C++ code,再写C code真不习惯
C++ interview questions help请教C++11的rvalue ref
用STL map的时候怎么自己定义大小比较的关系请教C++ call-by-ref & call-by-val的问题
一个C++ template的问题【讨论】问一道很简单的C++题。。。。 (转载)
相关话题的讨论汇总
话题: foo话题: obj话题: std话题: double话题: const
进入Programming版参与讨论
1 (共1页)
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.

1 (共1页)
进入Programming版参与讨论
相关主题
【讨论】问一道很简单的C++题。。。。 (转载)const int foo()啥意思?
为什么在overloading中,friend <<不能读取private值呢?C++ interview questions help
C++ namespace 弱问用STL map的时候怎么自己定义大小比较的关系
C++ linking 弱问 (one file)一个C++ template的问题
why use static function here?码工试题 (转载)
问个c++的template的问题有没有办法让一个类的变量只读,不是const?
请问关于overloading <<问个C++ 编译器临时变量的问题 (转载)
c++ does not check const for extern variable?What does the default constructor do?
相关话题的讨论汇总
话题: foo话题: obj话题: std话题: double话题: const