클래스 멤버 함수 내에 함수 객체를 정의하여 예를 들어 std :: transform 함수와 같이 직접 사용할 수 있는지 궁금합니다.
나는 그 예가 약간 어리 석다는 것을 안다. 내가 직면 한 문제를 보여 주기만하면된다.클래스 멤버 함수 내에서 함수 객체 선언 및 정의
파일 "example.h"
class Example {
public:
//.. constructor and destructor stuff
std::string toString() const; //Converts 'mVal' to a std::string
private:
std::vector<int> mVal; //Only one digit numbers are allowed ([0-9])
}
파일 "example.cpp"나는처럼 멤버 함수 내부에 직접 함수 객체를 구현하기 위해 노력 이래로
std::string Example::toString() const
{
//The functor which should be used in std::transform
struct {
char operator()(const int number) {
char c;
//"Convert" 'number' to a char
return c;
};
} functor;
//Transform the integers to char
std::string str(mVal.size(), '0'); //Allocate enough space
std::transform(mVal.begin(), mVal.end(), str.begin(), functor);
return str;
};//toString()
"example.cpp ", 코드가 컴파일되지 않습니다. 내가 오류 메시지는 다음과 같습니다
error: no matching function for call to ‘transform(__gnu_cxx::__normal_iterator<const int*, std::vector<int, std::allocator<int> > >, __gnu_cxx::__normal_iterator<const int*, std::vector<int, std::allocator<int> > >, __gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, Example::toString() const::<anonymous struct>&)’
그래서 내가 :: 변환 표준의 구조체 "펑"를 사용할 때 문제가 온다 생각합니다. 누군가 문제가 무엇인지 말해 줄 수 있습니까?
사용 : 우분투 리눅스에서
gcc-4.2 컴파일러를 사용 중입니다.
미리 감사드립니다.
René.
이의 http://stackoverflow.com/questions/2662843/c-can-local-class-reference-be-passed-to-a-function –
가능한 중복을 참조하십시오 [STL 알고리즘으로 로컬 클래스 사용하기] (http://stackoverflow.com/questions/742607/using-local-classes-with-stl-algorithms) –
네, 맞습니다. 정확한 검색어를 몰랐습니다. – PiJ