C++ 使用函数指针作为参数匹配模板失败

C++ 使用函数指针作为参数匹配模板失败,c++,templates,function-pointers,C++,Templates,Function Pointers,我正在尝试编写一个函数,该函数将对给定给它的任何键和类指针的std::map进行操作,并创建一个新的std::map,其中包含一个基于类中函数返回值的索引。本质上,是一个模板函数,用于根据映射所包含的类中的函数对映射重新编制索引。但是,我在尝试调用函数时遇到了编译器错误 template<class AnyType, class ValueType, class FunctionType> AssocArray<FunctionType,ValueType> reinde

我正在尝试编写一个函数,该函数将对给定给它的任何键和类指针的std::map进行操作,并创建一个新的std::map,其中包含一个基于类中函数返回值的索引。本质上,是一个模板函数,用于根据映射所包含的类中的函数对映射重新编制索引。但是,我在尝试调用函数时遇到了编译器错误

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;
}
其中,getB为浮点型

这将导致编译器错误:

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 (*)())
src/World.cpp:78:50:错误:调用“reindex(std::map&,float(onathacar::test::*)()”时没有匹配的函数
src/World.cpp:78:50:注:候选人为:
/include/Types.h:123:36:注意:模板std::map onathacar::reindex(const std::map&,FunctionType(*)()
我尝试了不同的变体,包括使用“FunctionType(ValueType::*getterFunction)(”)和将“AssocArray”更改为“AssocArray”。唯一有效的一个添加了第四个模板参数:

template<class AnyType, class ValueType, class FunctionType, class SomeType>
AssocArray<FunctionType,ValueType> reindex( const AssocArray<AnyType, ValueType>& original, FunctionType (SomeType::*getterFunction)() ) {
模板

AssocArray reindex(const AssocArray and.

似乎您正在尝试使用您的
rindex()
函数具有成员,但您的函数被声明为使用非成员。这不起作用。这不起作用的原因是您需要一个对象来访问函数或类的数据成员。

您有两个问题。第一个问题是
reindex
意味着值类型是值,但您将它们用作指针:

AssocArray<float, test*> floatIndexed;
floatIndexed = reindex( intIndexed, &test::getB );
模板
X类{
公众:
};
Y类{
公众:
int func(){return 42;}
};
样板
X reindex(constx&original,C(B::*getterFunction)(){
x2;

你能告诉我在没有第四个模板参数的情况下尝试FunctionType(ValueType::*getterFunction)()时遇到的错误吗?你的代码需要一个函数指针,而你给它传递了一个成员函数指针。你对这其中的哪一方面感到困惑?FunctionType(ValueType::*getterFunction)()错误似乎是因为我忘记将“AssocArray”返回值更改为“AssocArray”,这很有效;
template AssocArray reindex(const-AssocArray&original,FunctionType(ValueType::*getterFunction)()
模板/函数定义起作用了。我认为错误在于,当我尝试ValueType*时,我忘了将其更改为返回值。
AssocArray<float, test*> floatIndexed;
floatIndexed = reindex( intIndexed, &test::getB );
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;
}
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;
}