2012-02-10 3 views
1

주어진 키와 클래스 포인터의 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 ImplementationCalling Function입니다.

+0

네 번째 템플릿 매개 변수없이 FunctionType (ValueType :: * getterFunction)()을 사용하면 오류가 발생할 수 있습니까? – Sharad

+0

당신의 코드는 함수 포인터를 기대하며, 당신은 그것을 멤버 함수 포인터로 전달합니다. 특히이 부분의 어떤 측면을 혼동하고 있습니까? – ildjarn

+0

작동하는 "AssocArray "반환 값을 "AssocArray "변경 내용을 포함하는 것을 잊었 기 때문에 FunctionType (ValueType :: getterFunction)() 오류가 발생했습니다. '템플릿 AnyType, 클래스 ValueType, 클래스 FunctionType> AssocArray reindex (const AssocArray 및 원본, FunctionType (ValueType :: * getterFunction)())' – Bakaiya

답변

0

두 가지 문제가 있습니다. 첫 번째는 reindex은 값 유형이 값 있음을 의미한다는 것입니다,하지만 당신은 포인터로 사용 :

AssocArray<float, test*> floatIndexed; 
floatIndexed = reindex(intIndexed, &test::getB); 

두 번째는 reindex의 두 번째 매개 변수는 멤버 함수가 아닌 무료 함수로 선언 될 필요가 있다는 점이다. 그래서 reindex은 다음과 같아야합니다

template<class AnyType, class ValueType, class FunctionType> 
AssocArray<FunctionType,ValueType *> reindex(const AssocArray<AnyType, ValueType *>& original, FunctionType (ValueType:: *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; 
} 
+0

해당 템플릿/함수 정의가 작동했습니다. 그 오류는 ValueType *을 시도했을 때 반환 값으로 변경하는 것을 잊었다 고 생각합니다. – Bakaiya

0

rindex() 함수를 멤버와 함께 사용하려고했지만 멤버가 아닌 멤버를 사용하도록 함수가 선언 된 것으로 보입니다. 이것은 작동하지 않습니다. 이것이 작동하지 않는 이유는 클래스의 함수 또는 데이터 멤버에 액세스하는 객체가 필요하기 때문입니다.

0
template<class A, class B> 
class X{ 
public: 

}; 

class Y{ 
public: 
    int func() { return 42; } 
}; 

template<class A, class B, class C> 
X<C,B> reindex(const X<A, B>& original, C (B::*getterFunction)()) { 
    X<C, B> x2; 
     cout << "hello" << endl; 
    return x2; 
} 

int main() { X x1; reindex(x1,&Y::func); return 0;

}

이 작동 완벽하게 유효한 결과를 제공합니다.

AssocArray intIndexed 호출의 문제점은 valueType = test로 가정하는 getter 함수로 & test :: getB를 전달하려고한다는 사실입니다. 여기서 실제 값 유형은 test *입니다.

관련 문제