2012-12-09 25 views
2

我想模拟康威的生活游戏使用我创建的实现文件,我取得了很好的进展,但不幸的是我得到了一个让我困惑的错误。我认为这个问题是我如何正确的代码模板功能的一部分无知,反正这是我的实现文件:生活哈希表游戏.h文件模板问题

#include <list> 

#ifndef HashTable_h 
#define HashTable_h 

using namespace std; 

#define HASHTABLE_CAPACITY 1009 

template <class DataType> 
class HashTable 
{ 
    public: 
    HashTable(); // constructor 
    bool insert(DataType &a); // insert function for inserting value of dataType into table 
    bool retrieve(DataType &a); // retrieve function for retrieving value from table 
    bool replace(DataType &a); // function for replacing the value from the table with the parameter 
    bool remove(DataType& a);//removed function written and checked 
    //int getSizeOf() const; 
    void clear(); // for clearing the table 
    int size() const; 

    private: 
    list<DataType> table[HASHTABLE_CAPACITY]; // static array 
    int count; 
    int currentIndex; 
    typename list<DataType>::const_iterator it; 
}; 

// constructor 
template <class DataType> 
HashTable<DataType>::HashTable() 
{ 
    list<DataType> table[HASHTABLE_CAPACITY]; 
    count = 0; 
    currentIndex = -1; 
} 

// retrieve function 
template <class DataType> 
bool HashTable<DataType>::retrieve(DataType &a) 
{ 
    // get wrapped index 
    int wrappedIndex = a.hashCode() % HASHTABLE_CAPACITY; 
    if (wrappedIndex < 0) wrappedIndex = wrappedIndex + HASHTABLE_CAPACITY; 

    // if the array location isn't occupied, fail 
    if (wrappedIndex < 0 || wrappedIndex >= HASHTABLE_CAPACITY || table[wrappedIndex].empty()) return false; 

    // iterator for traversing table values 
    typename list<DataType>::const_iterator it; 

    // if the keys match then replace the data 
    // if a collision occurs then return false 
    it = find(table[wrappedIndex].begin(), table[wrappedIndex].end(), a); 
    if(it == table[wrappedIndex].end()) 
    return false; 
    a = *it; 

    return true; 
} 

// overloaded operator function 

// function for inserting values 
template <class DataType> 
bool HashTable<DataType>::insert(DataType &value) 
{ 
    // get wrapped index 
    int wrappedIndex = value.hashCode() % HASHTABLE_CAPACITY; 
    if (wrappedIndex < 0) wrappedIndex = wrappedIndex + HASHTABLE_CAPACITY; 

    // iterator for traversing values in table 
    typename list<DataType>::iterator it; 

    // if array location is not "occupied", copy into array 
    // else if keys match, replace the data 
    if (table[wrappedIndex].empty()) 
    { 
    table[wrappedIndex].push_back(value); 
    count++; 
    return true; 
    } 
    else 
    { 
    it = find(table[wrappedIndex].begin(), table[wrappedIndex].end(), value); 
    if (it != table[wrappedIndex].end()) *it = value; 
    else {table[wrappedIndex].push_back(value); count++;} 
    } 

    return true; 
} 

// function for replacing values 
template <class DataType> 
bool HashTable<DataType>::replace(DataType &value) 
{ 
    // get wrapped index 
    int wrappedIndex = value.hashCode() % HASHTABLE_CAPACITY; 
    if (wrappedIndex < 0) wrappedIndex = wrappedIndex + HASHTABLE_CAPACITY; 

    if(table[wrappedIndex].empty()) return false; 

    // iterator for traversing the values in table 
    typename list<DataType>::const_iterator it; 

    it = find(table[wrappedIndex].begin(), table[wrappedIndex].end(), value); 
    if(it == table[wrappedIndex].end()) return false; 

    value = *it; 
    table[wrappedIndex].erase(it); 
    count--; 

    return true; 
} 

template <class DataType> 
bool HashTable<DataType>::remove(DataType &value) 
{ 
    // get wrapped index 
    int wrappedIndex = value.hashCode() % HASHTABLE_CAPACITY; 
    if (wrappedIndex < 0) wrappedIndex = wrappedIndex + HASHTABLE_CAPACITY; 

    if(table[wrappedIndex].empty()) return false; 

    // iterator for traversing the values in table 
    typename list<DataType>::iterator it; 

    // if array location is not "occupied", copy into array 
    // else if keys match, remove the data 
    it = find(table[wrappedIndex].begin(), table[wrappedIndex].end(), value); 
    if(it == table[wrappedIndex].end()) return false; 

    value = *it; 
    table[wrappedIndex].erase(it); 
    count--; 

    return true; 
} 


// function for clearing the table of it's values 
template <class DataType> 
void HashTable<DataType>::clear() 
{ 
    count = 0; 
    currentIndex = -1; 

    for(int i = 0; i < HASHTABLE_CAPACITY; i++) 
    if(!table[i].empty()) table[i].clear(); 
} 

template <class DataType> 
int HashTable<DataType>::size() const 
{ 
    return count; 
} 

#endif 

这是实际的游戏生活驱动程序文件:

// Lab 11b 
#include <iostream> 
using namespace std; 

struct cell 
{ 
    int value; // equal to 1, so 0,0 is not a blank 
    int row; // any +/0/- value 
    int col; // any +/0/- value 

    bool operator==(const cell& c) const {return row == c.row && col == c.col;} 
    bool operator<(const cell& c) const {return (1000000 * row + col) < (1000000 * c.row + c.col);} 
    int hashCode() const {return 31 * row + col;} 
}; 

#include "HashTable.h" 
HashTable<cell> grid; 
HashTable<cell> newGrid; 

const int MINROW = -25; 
const int MAXROW = 25; 
const int MINCOL = -35; 
const int MAXCOL = 35; 

int neighborCount(int row, int col) 
{ 
    cell temp; 
    int count = 0; 
    for (temp.row = row - 1; temp.row <= row + 1; temp.row++) 
    for (temp.col = col - 1; temp.col <= col + 1; temp.col++) 
     if (temp.row != row || temp.col != col) 
     if (grid.retrieve(temp)) 
      ++count; 
    return count; 
} 

void initialize() 
{ 
    cout << "List the coordinates for living cells.\n"; 
    cout << "Terminate the list with a special pair -1 -1\n"; 

    cell temp; 
    while (true) 
    { 
    cin >> temp.row >> temp.col; 
    if (temp.row == -1 && temp.col == -1) break; 
    grid.insert(temp); 
    } 
    cin.ignore(); 
} 

void print() 
{ 
    cell temp = {1}; 
    cout << "\nThe current Life configuration is:\n"; 
    for (temp.row = MINROW; temp.row <= MAXROW; temp.row++) 
    { 
    for (temp.col = MINCOL; temp.col <= MAXCOL; temp.col++) 
     if (grid.retrieve(temp)) 
     cout << '*'; 
     else 
     cout << ' '; 
    cout << endl; 
    } 
    cout << endl; 
} 

void update() 
{ 
    cell temp = {1}; 
    newGrid.clear(); 
    for (temp.row = MINROW; temp.row <= MAXROW; temp.row++) 
    for (temp.col = MINCOL; temp.col <= MAXCOL; temp.col++) 
     switch (neighborCount(temp.row, temp.col)) 
     { 
     case 2: 
      if (grid.retrieve(temp)) newGrid.insert(temp); 
      break; 
     case 3: 
      newGrid.insert(temp); 
      break; 
     } 

    grid = newGrid;  
}; 

int main() 
{ 
    cout << "Welcome to Conway's game of Life\n"; 
    cout << "This game uses a grid in which\n"; 
    cout << "each cell can either be occupied by an organism or not.\n"; 
    cout << "The occupied cells change from generation to generation\n"; 
    cout << "according to the number of neighboring cells which are alive.\n"; 

    initialize(); 
    print(); 

    for (int i = 1; grid.size(); i++) 
    { 
    cout << "Generation " << i << ". Press ENTER to continue, X-ENTER to quit...\n"; 
    if (cin.get() > 31) break; 
    update(); 
    print(); 
    } 
    return 0; 
} 

当我试图编译这些文件我得到这个错误:

In file included from GameOfLife.cpp:16: 
HashTable.h: In member function ‘bool HashTable<DataType>::retrieve(DataType&) [with DataType = cell]’: 
GameOfLife.cpp:32: instantiated from here 
HashTable.h:74: error: no matching function for call to ‘find(std::_List_iterator<cell>, std::_List_iterator<cell>, cell&)’ 
HashTable.h: In member function ‘bool HashTable<DataType>::insert(DataType&) [with DataType = cell]’: 
GameOfLife.cpp:47: instantiated from here 
HashTable.h:117: error: no matching function for call to ‘find(std::_List_iterator<cell>, std::_List_iterator<cell>, cell&)’ 

这里有什么问题?

+1

是否有一个原因,你正在编写自己的散列表,而不是使用'std :: unordered_set'? –

回答

3

您需要#include <algorithm>才能获得std::find。这大概是你打电话给find时要用的。您应该避免使用using namespace std,特别是在标题中。

+0

好吧,现在正在工作,谢谢! – newwarrior21st