C++ 使用时出现错误C2064<;功能性>;及<;绑定>;

C++ 使用时出现错误C2064<;功能性>;及<;绑定>;,c++,hashtable,bind,function-pointers,member-initialization,C++,Hashtable,Bind,Function Pointers,Member Initialization,我真的不知道在这里该怎么办。我查到的每个答案都有我不懂的语法 错误: Error 1 error C2064: term does not evaluate to a function taking 1 arguments 我在哈希表构造函数中使用函数指针。有人建议我使用和标题来解决我遇到的问题。它解决了错误,但我遇到了上面的错误 我的哈希表声明和ctor如下: #pragma once #include "SLList.h" template<typename Type> cl

我真的不知道在这里该怎么办。我查到的每个答案都有我不懂的语法

错误:

Error 1 error C2064: term does not evaluate to a function taking 1 arguments
我在哈希表构造函数中使用函数指针。有人建议我使用和标题来解决我遇到的问题。它解决了错误,但我遇到了上面的错误

我的哈希表声明和ctor如下:

#pragma once
#include "SLList.h"

template<typename Type> class HTable
{
public:
     HTable(unsigned int numOfBuckets, std::function<unsigned int(const Type&)>           hFunction);
    ~HTable();
    HTable<Type>& operator=(const HTable<Type>& that);
    HTable(const HTable<Type>& that);
    void insert(const Type& v);
    bool findAndRemove(const Type& v);
    void clear();
    int find(const Type& v) const;

private:
    SLList<Type>* ht;
    std::function<unsigned int(const Type&)> hFunct;
    unsigned int numOfBuck;
}; 

template<typename Type>
HTable<Type>:: HTable(unsigned int numOfBuckets, std::function<unsigned int(const     Type&)> hFunction)
{
    ht = new SLList<Type>[numOfBuckets];
    this->numOfBuck = numOfBuckets;
    this->hFunct = hFunction;
} 

提前感谢。

xorHash
是一个接受1个参数的方法。这意味着它还隐式地接受一个
this
指针


使其成为
静态
方法或
类之外的自由函数

要传递的哈希函数对象仍然需要将要哈希的值作为参数。那就是你想要绑定像这样的东西

std::bind(&Game::xorHash, this, std::placeholders::_1)
需要
\u 1
-位来告诉
std::bind()
一个参数必须去哪里以及要发送到哪里(在本文中,哪一个不是很有趣,因为只有一个;如果您绑定的是要接收多个参数的函数,则更有趣)


请注意,您实际上不太可能希望传递一个真正的成员函数:通常,计算的哈希值不依赖于对象状态,也就是说,您最好创建类的
xorHash()
a
static
member函数并传入该函数:这样您甚至不需要
std::bind()
任何参数。

未绑定函数参数需要一个占位符:

std::bind(&Game::xorHash, this, std::placeholders::_1)
根据口味,lambda可能更具可读性:

[this](const std::string & s){return xorHash(s);}

虽然我不清楚为什么
xorHash
需要成为非静态成员;当然,散列应该只依赖于它的输入?

这解决了这个问题。不幸的是,我不能太担心引擎盖下到底发生了什么,因为我必须在周一之前把这整件事写完,而我的导师几乎没有给出这方面的指导。在编写实际的xorHash函数时,我需要知道什么特别的东西吗?如果它不需要是非静态成员,那就好了。否则,您可能希望在OP尝试时使用
函数
绑定
std::bind(&Game::xorHash, this, std::placeholders::_1)
[this](const std::string & s){return xorHash(s);}