주어진 키와 클래스 포인터의 std :: map에서 작동하는 함수를 작성하려고합니다. 그리고 인덱스 기반의 새로운 std :: map을 만듭니다. 클래스에있는 함수의 반환 값. 기본적으로 포함 된 클래스의 함수를 기반으로지도의 색인을 다시 생성하는 템플릿 함수입니다. 그러나 함수를 호출 할 때 컴파일러 오류가 발생합니다. getB는 플로트 타입이다함수 포인터를 인수로 사용하여 템플릿을 일치시키지 못했습니다.
floatIndexed = reindex(intIndexed, &test::getB);
:
template<class AnyType, class ValueType, class FunctionType>
AssocArray<FunctionType,ValueType> reindex(const AssocArray<AnyType, ValueType>& original, FunctionType (*getterFunction)()) {
AssocArray<FunctionType, ValueType> ret;
FunctionType index;
for(typename AssocArray<AnyType,ValueType>::const_iterator it = original.begin(); it!=original.end(); it++) {
index = ((*it->second).*getterFunction)();
ret[index] = it->second;
}
return ret;
}
에 의해 불려갑니다.
이 컴파일러 오류가 발생합니다 : 나는 "(치 형 : * getterFunction)() FunctionType"을 사용하여 "AssocArray"을 "AssocArray을"변화를 포함하여 다양한 변화를 시도
src/World.cpp:78:50: error: no matching function for call to ‘reindex(std::map<int, onathacar::test*>&, float (onathacar::test::*)())’
src/World.cpp:78:50: note: candidate is:
./include/Types.h:123:36: note: template<class AnyType, class ValueType, class FunctionType> std::map<PreservedType, ValueType> onathacar::reindex(const std::map<LookupType, ValueType>&, FunctionType (*)())
. 잠재적 실제로 호출되는 치형의 구성원이 아닌 기능을 허용 것이고, 그래서 다른 옵션을 선호 할 것 같은
template<class AnyType, class ValueType, class FunctionType, class SomeType>
AssocArray<FunctionType,ValueType> reindex(const AssocArray<AnyType, ValueType>& original, FunctionType (SomeType::*getterFunction)()) {
그러나이 보인다 : 일 유일한 사람은 제 4 템플릿 인수를 추가했다. 적어도 "ValueType ::"을 추가하면 템플릿이 일치하는 것으로 보이므로 무엇이 잘못되었는지조차 확신하지 못합니다. 호출이 템플릿과 일치하지 않는 이유는 무엇입니까? 네 번째 템플릿 형식을 사용하지 않고 호출을 수정하는 방법이 있습니까?
자세한 내용은 Header Containing Implementation 및 Calling Function입니다.
네 번째 템플릿 매개 변수없이 FunctionType (ValueType :: * getterFunction)()을 사용하면 오류가 발생할 수 있습니까? – Sharad
당신의 코드는 함수 포인터를 기대하며, 당신은 그것을 멤버 함수 포인터로 전달합니다. 특히이 부분의 어떤 측면을 혼동하고 있습니까? – ildjarn
작동하는 "AssocArray"반환 값을 "AssocArray "변경 내용을 포함하는 것을 잊었 기 때문에 FunctionType (ValueType :: getterFunction)() 오류가 발생했습니다. '템플릿 AnyType, 클래스 ValueType, 클래스 FunctionType> AssocArray reindex (const AssocArray 및 원본, FunctionType (ValueType :: * getterFunction)())' –
Bakaiya