Ahoj, mám přibližně takovýto kód:
#include <iostream>
#include <algorithm>
#include <vector>
#include <utility>
typedef std::pair<std::size_t, std::size_t> point;
void print( point i) {
std::cout << i.first << ", ";
}
class A {
public:
A( std::vector< point >& v) {
std::for_each(v.begin(), v.end(), ::print );
std::cout << std::endl;
//std::for_each(v.begin(), v.end(), this->print2 );
std::cout << std::endl;
}
void print2 ( point i ) {
std::cout << i.first << ", ";
}
};
int main(void) {
std::vector< point > vec;
vec.push_back( point(3,3) );
vec.push_back( point(4,4) );
A a( vec );
return 0;
}
Což normálně zkompiluji:
g++ -std=c++0x -Wall -pedantic test.cpp
clang++ test.cpp
Když odkomentuji řádek 17, tak již nezkompiluji. Nicméně já bych v tom for_each právě potřeboval volat členskou funkci dané třídy.
Chybový výpis g++:
test.cpp: In constructor ‘A::A(std::vector<std::pair<long unsigned int, long unsigned int> >&)’:
test.cpp:17:50: error: argument of type ‘void (A::)(point) {aka void (A::)(std::pair<long unsigned int, long unsigned int>)}’ does not match ‘void (A::*)(std::pair<long unsigned int, long unsigned int>)’
In file included from /usr/include/c++/4.6/algorithm:63:0,
from test.cpp:2:
/usr/include/c++/4.6/bits/stl_algo.h: In function ‘_Funct std::for_each(_IIter, _IIter, _Funct) [with _IIter = __gnu_cxx::__normal_iterator<std::pair<long unsigned int, long unsigned int>*, std::vector<std::pair<long unsigned int, long unsigned int> > >, _Funct = void (A::*)(std::pair<long unsigned int, long unsigned int>)]’:
test.cpp:17:50: instantiated from here
/usr/include/c++/4.6/bits/stl_algo.h:4302:2: error: must use ‘.*’ or ‘->*’ to call pointer-to-member function in ‘__f (...)’, e.g. ‘(... ->* __f) (...)’
Chybový výpis clang++:
test.cpp:17:3: error: no matching function for call to 'for_each'
std::for_each(v.begin(), v.end(), this->print2 );
^~~~~~~~~~~~~
In file included from test.cpp:2:
In file included from /usr/include/c++/4.6/algorithm:62:
/usr/include/c++/4.6/bits/stl_algo.h:4296:5: note: candidate function [with _InputIterator =
__gnu_cxx::__normal_iterator<std::pair<unsigned long, unsigned long> *, std::vector<std::pair<unsigned
long, unsigned long>, std::allocator<std::pair<unsigned long, unsigned long> > > >, _Function = void
(*)(std::pair<unsigned long, unsigned long>)] not viable: no known conversion from 'void (point)' to 'void
(*)(std::pair<unsigned long, unsigned long>)' for 3rd argument
for_each(_InputIterator __first, _InputIterator __last, _Function __f)
^
1 error generated.