Search code examples
c++hashtablebindfunction-pointersmember-initialization

Error C2064 using <functional> and <bind>


I really don't know what to do here. Every answer I look up has syntax that I just don't understand.

error:

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

I'm using a function pointer in a hash table constructor. It was suggested that I use the and headers to solve an issue I was having. It solved the errors, but I ran into the above error.

my Hash Table declaration and ctor are as follows:

#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 (containing the table):

#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 (I am attempting to pass in the xorHash function)

#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;
}

Thanks in advance.


Solution

  • You need a placeholder for the unbound function argument:

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

    A lambda might be more readable, according to taste:

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

    Although it's not clear to me why xorHash needs to be a non-static member at all; surely, a hash should depend only on its input?