0

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.

rearden
  • 135
  • 1
  • 1
  • 11

3 Answers3

0

xorHash is a method that takes 1 argument. That means it also implicitly takes a this pointer.

Make it a static method or a free function outside a class.

Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524
  • That's fine if it doesn't need to be a non-static member. Otherwise, you'd want to use `function` and `bind` as the OP is attempting. – Mike Seymour Nov 09 '13 at 20:08
0

The hash function object you want to pass still needs to take the value to hash as argument. That is you want to bind something like

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

The _1-bit is needed to tell std::bind() where an argument has to go and which one to send there (which one isn't that interesting in this context as there will be only one; it is more interesting if you are binding functions which are to receive multiple arguments).

Note that it is actually unlikely that you want to pass a real member function: Normally, the hash value computed doesn't depend on the object state, i.e., you are probably better off making xorHash() a static member function of your class and pass this one in: this way you don't even need to std::bind() any parameters.

Dietmar Kühl
  • 150,225
  • 13
  • 225
  • 380
  • This fixed the issue. Unfortunately, I can't worry too much about what is going on underneath the hood, since I have to get this entire thing written by Monday, and my instructor gave next to no direction for this. Is there anything special I need to know when writing the actual xorHash function? – rearden Nov 09 '13 at 20:06
0

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?

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644