2013-11-09 22 views
0

我有一個散列表模板,我已經寫了一個類。我有一個項目應該利用這個哈希表。它接受一個無符號的整數值來初始化它所具有的桶的數量,以及一個指向的哈希函數。我還沒有寫這個哈希函數,但是我有一個聲明。當我嘗試在我的Game類中爲哈希表數據成員使用成員初始值設定項時,它給了我一個我不明白的錯誤。哈希表不接受函數傳入構造函數在成員初始化列表

Error 1 error C3867: 'Game::xorHash': function call missing argument list; use '&Game::xorHash' to create a pointer to member 

2 IntelliSense: no instance of constructor "HTable<Type>::HTable [with Type=std::string]" matches the argument list 
     argument types are: (int, unsigned int (const std::string &s)) 

我的哈希表類如下:

#pragma once 
#include "SLList.h" 

template<typename Type> class HTable 
{ 
public: 
    HTable(unsigned int numOfBuckets, unsigned int (*hFunction) (const Type &v)); 
    ~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; 
    unsigned int (*hFunct) (const Type &v); 
    unsigned int numOfBuck; 
}; 

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

template<typename Type> 
HTable<Type>::~HTable() 
{ 
    delete [] ht; 
    ht = nullptr; 
} 

template<typename Type> 
HTable<Type>& HTable<Type>::operator=(const HTable<Type>& that) 
{ 
    if(this != &that) 
    { 
     delete [] this->ht; 
     this->hFunct = that.hFunct; 
     this->numOfBuck = that.numOfBuck; 
     this->ht = new SLList<Type>[numOfBuck]; 
     for(unsigned int i = 0; i < this->numOfBuck; i++) 
      this->ht[i] = that.ht[i]; 
    } 
    return *this; 
} 

template<typename Type> 
HTable<Type>::HTable(const HTable<Type>& that) 
{ 
    this = *that; 
} 

template<typename Type> 
void HTable<Type>::insert(const Type& v) 
{ 
    ht[hFunct(v)].addHead(v); 
} 

template<typename Type> 
bool HTable<Type>::findAndRemove(const Type& v) 
{ 
    SLLIter<Type> iter(ht[hFunct(v)]); 
    for(iter.begin(); !iter.end(); ++iter) 
    { 
     if(v == iter.current()) 
     { 
      ht[hFunct(v)].remove(iter); 
      return true; 
     } 
    } 
    return false; 
} 

template<typename Type> 
void HTable<Type>::clear() 
{ 
    for(unsigned int i = 0; i < this->numOfBuck; ++i) 
     ht[i].clear(); 
} 

template<typename Type> 
int HTable<Type>::find(const Type& v) const 
{ 
    SLLIter<Type> iter(ht[hFunct(v)]); 
    for(iter.begin(); !iter.end(); ++iter) 
    { 
     if(v == iter.current()) 
      return hFunct(v); 
    } 

    return -1; 
} 

我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(這顯然只是一個骨架,因爲我可以」 t讓成員初始化工作)

#include "Game.h" 


Game::Game(void) : dictionary(2048, xorHash) 
{ 

} 


Game::~Game(void) 
{ 

} 

void Game::refresh() 
{ 

} 

void Game::input() 
{ 

} 

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

我一直在爲此工作很長一段時間,並且一直在撞牆。我真的很感謝一些關於如何讓這個東西啓動和運行的幫助。讓我知道是否有另一個需要看到的片段(我試圖在這方面徹底)。

回答

1

你有兩個問題。首先是你沒有正確傳遞成員函數指針(錯誤信息告訴你到底做了什麼)。另一個問題是功能指針與成員函數指針不一樣。

成員函數指針需要一個實例對象對象來調用成員函數。這個實例作爲隱藏的第一個參數傳遞,這是普通函數沒有的。

爲此,您可能會轉而用std::functionstd::bind

class HTable 
{ 
public: 
    HTable(unsigned int numOfBuckets, std::function<unsigned int(const Type&)> hFunction); 
    ... 

private: 
    std::function<unsigned int(const Type&)> hFunct; 
    ... 
}; 

然後

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

這絕對清理錯誤。我很抱歉,如果下一個是一個基本問題,但我從來沒有使用函數或綁定標題。我已經實現了您的建議並在功能標題中遇到錯誤: 錯誤1錯誤C2064:術語不會評估爲帶有1個參數的函數 – rearden

相關問題