unfortunately I can't even get this simple code using just one parameter to compile:
Code:
#include <vector>
#include <cstdio>
#include <string>
#include <functional>
#include <algorithm>
struct A
{
void print_sum(int x, int y) const
{
printf("%d\n",x+y);
}
};
void call_print_sum( const A& a, int z)
{
a.print_sum(1,z);
}
void worker_function(int x, int y)
{
std::vector<A> vec;
A a1, a2, a3;
vec.push_back(a1);
vec.push_back(a2);
vec.push_back(a3);
std::for_each( vec.begin(), vec.end(), std::bind2nd(call_print_sum, 3) );
}
int main()
{
worker_function(3,7);
}
The linker says:
Code:
make all
Building file: ../main.cpp
Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"main.d" -MT"main.d" -o"main.o" "../main.cpp"
/usr/lib/gcc/i686-pc-linux-gnu/4.1.2/include/g++-v4/bits/stl_function.h: In instantiation of ‘std::binder2nd<void ()(const A&, int)>’:
../main.cpp:28: instantiated from here
/usr/lib/gcc/i686-pc-linux-gnu/4.1.2/include/g++-v4/bits/stl_function.h:429: error: ‘void ()(const A&, int)’ is not a class, struct, or union type
/usr/lib/gcc/i686-pc-linux-gnu/4.1.2/include/g++-v4/bits/stl_function.h:432: error: ‘void ()(const A&, int)’ is not a class, struct, or union type
/usr/lib/gcc/i686-pc-linux-gnu/4.1.2/include/g++-v4/bits/stl_function.h:435: error: ‘void ()(const A&, int)’ is not a class, struct, or union type
/usr/lib/gcc/i686-pc-linux-gnu/4.1.2/include/g++-v4/bits/stl_function.h:439: error: ‘void ()(const A&, int)’ is not a class, struct, or union type
/usr/lib/gcc/i686-pc-linux-gnu/4.1.2/include/g++-v4/bits/stl_function.h:445: error: ‘void ()(const A&, int)’ is not a class, struct, or union type
/usr/lib/gcc/i686-pc-linux-gnu/4.1.2/include/g++-v4/bits/stl_function.h:431: error: field ‘std::binder2nd<void ()(const A&, int)>::op’ invalidly declared function type
/usr/lib/gcc/i686-pc-linux-gnu/4.1.2/include/g++-v4/bits/stl_function.h: In function ‘std::binder2nd<_Operation> std::bind2nd(const _Operation&, const _Tp&) [with _Operation = void ()(const A&, int), _Tp = int]’:
../main.cpp:28: instantiated from here
/usr/lib/gcc/i686-pc-linux-gnu/4.1.2/include/g++-v4/bits/stl_function.h:454: error: ‘void ()(const A&, int)’ is not a class, struct, or union type
/usr/lib/gcc/i686-pc-linux-gnu/4.1.2/include/g++-v4/bits/stl_function.h:455: error: ‘void ()(const A&, int)’ is not a class, struct, or union type
/usr/lib/gcc/i686-pc-linux-gnu/4.1.2/include/g++-v4/bits/stl_algo.h: In function ‘_Function std::for_each(_InputIterator, _InputIterator, _Function) [with _InputIterator = __gnu_cxx::__normal_iterator<A*, std::vector<A, std::allocator<A> > >, _Function = std::binder2nd<void ()(const A&, int)>]’:
../main.cpp:28: instantiated from here
/usr/lib/gcc/i686-pc-linux-gnu/4.1.2/include/g++-v4/bits/stl_algo.h:159: error: no match for call to ‘(std::binder2nd<void ()(const A&, int)>) (A&)’
make: *** [main.o] Error 1
Does anybody know why?