때로는 목록을 조작하기 위해 일부 functor-helper가 필요합니다. 범위를 가능한 한 로컬로 유지하려고합니다.함수 안에 함수를 정의하는 방법
hello.cpp: In function ‘int main()’:
hello.cpp:18:34: error: no matching function for call to ‘transform(int [5], int*, int [5], main()::Square)’
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
struct Square
{
int operator()(int x)
{
return x*x;
}
};
int a[5] = {0, 1, 2, 3, 4};
int b[5];
transform(a, a+5, b, Square());
for(int i=0; i<5; i++)
cout<<a[i]<<" "<<b[i]<<endl;
}
내가
main()
에서
Square
를 이동하는 경우, 그것은 괜찮습니다.
[이 질문 참조] (http://stackoverflow.com/questions/6880077/why-does-this-stdsort-predicate-fail-when-the-class-is-inside-main). – hammar
흠 ... 질문에서 코드를 복사하여 VS2010에서 시도해 보았지만 정상적으로 작동합니다. : - \. – TCS