0

我真的不知道在这里做什么。我查找的每个答案都有我不理解的语法。

错误:

Error 1 error C2064: term does not evaluate to a function taking 1 arguments

我在哈希表构造函数中使用函数指针。有人建议我使用 and 标头来解决我遇到的问题。它解决了错误,但我遇到了上述错误。

我的哈希表声明和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;
} 

Game.h(包含表格):

#pragma once

#include "stdafx.h"
#include "HTable.h"
#include "BST.h"
#include "DTSTimer.h"

using namespace std;

class Game
{
public:
    Game(void);
    virtual ~Game(void);
    void refresh();
    void input();
    unsigned int xorHash(const string &s);

private:
    string userInput;
    DTSTimer timer;
    BST<string> answers;
    HTable<string> dictionary;
}; 

Game.cpp(我正在尝试传入 xorHash 函数)

#include "Game.h"


Game::Game(void) : dictionary(2048, std::bind(&Game::xorHash, this))
{

}


Game::~Game(void)
{

}

void Game::refresh()
{

}

void Game::input()
{

}

unsigned int Game::xorHash(const string &s)
{
    return 0;
}

提前致谢。

4

3 回答 3

0

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

使其成为 astatic之外的方法或自由函数class

于 2013-11-09T20:02:15.383 回答
0

您要传递的散列函数对象仍然需要将要散列的值作为参数。那就是你想绑定类似的东西

std::bind(&Game::xorHash, this, std::placeholders::_1)

_1需要 -bit 来告诉参数必须去哪里以及将std::bind()哪个参数发送到那里(在这种情况下,哪个参数并不那么有趣,因为只有一个参数;如果您要绑定要接收的函数,那就更有趣了多个参数)。

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

于 2013-11-09T20:02:27.030 回答
0

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

std::bind(&Game::xorHash, this, std::placeholders::_1)

根据口味,lambda 可能更具可读性:

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

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

于 2013-11-09T20:03:35.703 回答