2013-08-15 1 views
2

C++ 11을 사용할 수 없습니다.std :: for_each를 멤버 함수와 함께 사용하는 방법. 제공된 데이터 구조가 객체가 아니라 매개 변수를 제공합니다.

일반적으로 std :: for_each를 멤버 함수와 함께 사용하면 제공된 데이터 구조가 바인딩 된 멤버 함수가 호출되는 개체를 제공합니다. 그러나 루프 내내 상수 인 내 자신의 개체를 제공 할 수 있는지 알고 싶습니다 및이 멤버 함수에 매개 변수에 대한 데이터 구조를 사용하십시오.

다음은 내가 원하는 것의 예입니다.

std::string GenerateKey(const std::vector<std::string>& parameters) 
{ 
    std::string key; 
    key.reserve(std::accumulate(parameters.begin(), parameters.end(), 0, std::mem_fun_ref(&std::string::length))); 
    std::for_each(parameters.begin(), parameters.end(), std::bind1st(std::mem_fun_ref(&std::string::append), key)); 
    return key; 
} 

매개 변수의 각 문자열에 대해 append가 key에서 호출되고 매개 변수의 문자열이 전달됩니다.

왜이 std :: for_each가 작동하지 않으며 어떻게 작동시킬 수 있습니까?

문자열을 연결하는 다른 방법이 있다는 것을 알고 있습니다. C++ 함수 프로그래밍을 더 잘 이해하고 싶습니다.

감사합니다. 여기

은 .. 내가 도움이 될 것입니다 의심 VS2013에서 오류 메시지입니다

generator.cpp(28): error C2784: 'std::const_mem_fun1_ref_t<_Result,_Ty,_Arg> std::mem_fun_ref(_Result (__thiscall _Ty::*)(_Arg) const)' : could not deduce template argument for 'overloaded function type' from 'overloaded function type' 
      c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(910) : see declaration of 'std::mem_fun_ref' 
      c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(910) : see declaration of 'std::mem_fun_ref' 
      c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(910) : see declaration of 'std::mem_fun_ref' 
      c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(910) : see declaration of 'std::mem_fun_ref' 
      c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(910) : see declaration of 'std::mem_fun_ref' 
      c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(910) : see declaration of 'std::mem_fun_ref' 
      c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(910) : see declaration of 'std::mem_fun_ref' 
      c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(910) : see declaration of 'std::mem_fun_ref' 
generator.cpp(28): error C2783: 'std::const_mem_fun1_ref_t<_Result,_Ty,_Arg> std::mem_fun_ref(_Result (__thiscall _Ty::*)(_Arg) const)' : could not deduce template argument for '_Result' 
      c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(910) : see declaration of 'std::mem_fun_ref' 
generator.cpp(28): error C2783: 'std::const_mem_fun1_ref_t<_Result,_Ty,_Arg> std::mem_fun_ref(_Result (__thiscall _Ty::*)(_Arg) const)' : could not deduce template argument for '_Ty' 
      c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(910) : see declaration of 'std::mem_fun_ref' 
generator.cpp(28): error C2783: 'std::const_mem_fun1_ref_t<_Result,_Ty,_Arg> std::mem_fun_ref(_Result (__thiscall _Ty::*)(_Arg) const)' : could not deduce template argument for '_Arg' 
      c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(910) : see declaration of 'std::mem_fun_ref' 
generator.cpp(28): error C2784: 'std::const_mem_fun_ref_t<_Result,_Ty> std::mem_fun_ref(_Result (__thiscall _Ty::*)(void) const)' : could not deduce template argument for 'overloaded function type' from 'overloaded function type' 
      c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(901) : see declaration of 'std::mem_fun_ref' 
      c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(901) : see declaration of 'std::mem_fun_ref' 
      c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(901) : see declaration of 'std::mem_fun_ref' 
      c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(901) : see declaration of 'std::mem_fun_ref' 
      c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(901) : see declaration of 'std::mem_fun_ref' 
      c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(901) : see declaration of 'std::mem_fun_ref' 
      c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(901) : see declaration of 'std::mem_fun_ref' 
      c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(901) : see declaration of 'std::mem_fun_ref' 
generator.cpp(28): error C2783: 'std::const_mem_fun_ref_t<_Result,_Ty> std::mem_fun_ref(_Result (__thiscall _Ty::*)(void) const)' : could not deduce template argument for '_Result' 
      c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(901) : see declaration of 'std::mem_fun_ref' 
generator.cpp(28): error C2783: 'std::const_mem_fun_ref_t<_Result,_Ty> std::mem_fun_ref(_Result (__thiscall _Ty::*)(void) const)' : could not deduce template argument for '_Ty' 
      c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(901) : see declaration of 'std::mem_fun_ref' 
generator.cpp(28): error C2784: 'std::mem_fun1_ref_t<_Result,_Ty,_Arg> std::mem_fun_ref(_Result (__thiscall _Ty::*)(_Arg))' : could not deduce template argument for 'overloaded function type' from 'overloaded function type' 
      c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(893) : see declaration of 'std::mem_fun_ref' 
      c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(893) : see declaration of 'std::mem_fun_ref' 
      c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(893) : see declaration of 'std::mem_fun_ref' 
      c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(893) : see declaration of 'std::mem_fun_ref' 
      c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(893) : see declaration of 'std::mem_fun_ref' 
generator.cpp(28): error C2914: 'std::mem_fun_ref' : cannot deduce template argument as function argument is ambiguous 
generator.cpp(28): error C2783: 'std::mem_fun1_ref_t<_Result,_Ty,_Arg> std::mem_fun_ref(_Result (__thiscall _Ty::*)(_Arg))' : could not deduce template argument for '_Result' 
      c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(893) : see declaration of 'std::mem_fun_ref' 
generator.cpp(28): error C2783: 'std::mem_fun1_ref_t<_Result,_Ty,_Arg> std::mem_fun_ref(_Result (__thiscall _Ty::*)(_Arg))' : could not deduce template argument for '_Ty' 
      c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(893) : see declaration of 'std::mem_fun_ref' 
generator.cpp(28): error C2783: 'std::mem_fun1_ref_t<_Result,_Ty,_Arg> std::mem_fun_ref(_Result (__thiscall _Ty::*)(_Arg))' : could not deduce template argument for '_Arg' 
      c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(893) : see declaration of 'std::mem_fun_ref' 
generator.cpp(28): error C2784: 'std::mem_fun_ref_t<_Result,_Ty> std::mem_fun_ref(_Result (__thiscall _Ty::*)(void))' : could not deduce template argument for 'overloaded function type' from 'overloaded function type' 
      c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(884) : see declaration of 'std::mem_fun_ref' 
      c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(884) : see declaration of 'std::mem_fun_ref' 
      c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(884) : see declaration of 'std::mem_fun_ref' 
      c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(884) : see declaration of 'std::mem_fun_ref' 
      c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(884) : see declaration of 'std::mem_fun_ref' 
      c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(884) : see declaration of 'std::mem_fun_ref' 
      c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(884) : see declaration of 'std::mem_fun_ref' 
      c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(884) : see declaration of 'std::mem_fun_ref' 
generator.cpp(28): error C2783: 'std::mem_fun_ref_t<_Result,_Ty> std::mem_fun_ref(_Result (__thiscall _Ty::*)(void))' : could not deduce template argument for '_Result' 
      c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(884) : see declaration of 'std::mem_fun_ref' 
generator.cpp(28): error C2783: 'std::mem_fun_ref_t<_Result,_Ty> std::mem_fun_ref(_Result (__thiscall _Ty::*)(void))' : could not deduce template argument for '_Ty' 
      c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(884) : see declaration of 'std::mem_fun_ref' 
generator.cpp(28): error C2780: '_Fn1 std::for_each(_InIt,_InIt,_Fn1)' : expects 3 arguments - 2 provided 
      c:\program files\microsoft visual studio 12.0\vc\include\algorithm(29) : see declaration of 'std::for_each' 
+1

'for_each'을 (를) 사용 하시겠습니까? 왜'std :: ostringstream'을'std :: ostream_iterator'와'std :: copy'와 함께 사용하지 않을까요? –

+0

@JoachimPileborg 왜 코드가 작동하지 않는지 이해하고 싶습니다. 문자열은 예제입니다. –

답변

1

std::string::append 과부하 및 컴파일러는 의미 기능을 알 수 없습니다.

따라서 std::string::append은 실제로 일련의 기능을 나타냅니다.

+0

이것이 불가능하다는 뜻입니까? –

0

가이 오류 때문에이 오버로드 된 버전 기능 표준 : : 문자열 :: APPEND 중 일부는 사용할 필요 exacly 기능 컴파일러에게 캐스트를 사용할 수 있습니다 :

std::for_each(parameters.begin(), parameters.end(), std::bind1st(std::mem_fun_ref(static_cast<std::string& (std::string::*)(const std::string&)>(&std::string::append)), key)); 

편집 :하지만이 경우 bind1st는 매개 변수로 참조를 취하는 함수에서 작동하지 않으므로 작동하지 않습니다. 참조 : Using bind1st for a method that takes argument by reference

+0

나는 컴파일러가 다시 행복하다고 생각하지 않는다 – P0W

+0

'c : ₩ program files ₩ microsoft visual studio 12.0 \ vc \ include \ xfunctional (465) : 오류 C2535 : 'std :: basic_string , std :: allocator & std :: binder1st , std :: allocator > const std :: string & >> : : operator() (const std :: basic_string , std :: allocator >) const ': 멤버 함수가 이미 선언되었거나 선언되었습니다.' –

+0

@NeilKirk 내 대답을 편집합니다. 다른 오류가 있습니다. 다른 원인과 나는이 경우 더 좋은 해결책을 모른다. – user1837009

관련 문제