C 将std ::函数从另一个类带到构造函数
c++ passing std::function from another class to a constructor
我将方法传递到构造函数中,但是当我使用另一个类的方法时,它会给我一个错误。
当前正在以此形式工作
unsigned int pHash(const std::string& str)
{
//method
}
int main()
{
//stuff
HashMap* hm2 = new HashMap(pHash);
//stuff
}
但是,如果我引用另一个标头文件的方法,则
HashMap* hm2 = new HashMap(&HashFcn::primeHash);
我在运行时会出错,并带有以下消息;
Error 1 error C2100: illegal indirection c:program files (x86)microsoft visual studio 11.0vcincludexrefwrap 273 1 Project
Error 2 error C2440: 'newline' : cannot convert from 'const std::string *' to 'const HashFcn *' c:program files (x86)microsoft visual studio 11.0vcincludexrefwrap 273 1 Project
Error 3 error C2647: '.*' : cannot dereference a 'unsigned int (__thiscall HashFcn::* )(const std::string &)' on a 'const std::string' c:program files (x86)microsoft visual studio 11.0vcincludexrefwrap 273 1 Project
我的hashmap构造函数看起来像这样;
HashMap::HashMap(HashFunction hashFunction)
: hfunc(hashFunction)
其中hfunc是方法的类型。
我有一个具有PrimeHash方法的hashfcn
unsigned int primeHash(const std::string&);
这是我第一次进行Typdef/方法传递 - 任何线索或帮助都将不胜感激!
尝试使PrimeHash静态:
static unsigned int primeHash(const std::string&);
相关文章:
- 如果有一个模板构造函数只有一个泛型参数,为什么我必须有一个复制构造函数
- 关于复制构造函数的一个棘手问题
- C++构造函数,一个用于度,一个用于弧度
- 关于QLIST类型实例复制构造函数的一个奇怪问题
- 需要使班级(long_number)用构造函数和一个打印功能代表1到60的任何长度
- 运算符 = 不匹配,构造函数创建一个指针
- 为什么我不能在 c++ 类中声明一个空构造函数,该构造函数从一个具有私有构造函数的构造函数扩展而来
- 如何使构造函数接受一个非常量对象
- 初始值设定项列表,可变参数模板和构造函数推导:一个奇怪的情况
- shared_ptr的原始指针构造函数是一个错误吗?
- 当这个构造函数接受一个初始值设定项列表并委托一个向量时,是什么导致它委托给自己
- C++11使variadic构造函数了解一个初始化列表的初始化列表
- 为什么这个initializer_list构造函数是一个可行的重载
- 为什么我的Arduino类构造函数需要一个参数
- 内联构造函数和一个定义规则
- 如何通过构造函数将一个类的实例传递给另一个类
- 如何用g++构造在另一个文件中定义的类
- 在另一个类中使用自定义构造函数声明一个类
- 如何从另一个构造函数调用一个c++类的构造函数
- 模板中的构造函数不同:一个用于字符串,另一个用于其他任何东西