2012-12-09 2 views
2

내가 만든 구현 파일을 사용하여 Conway의 게임을 시뮬레이션하려고하는데, 나는 진전을 이루었지만 불행히도 나를 혼란스럽게하는 오류가 발생했습니다. 나는 문제가 제대로 템플릿 기능을 코딩하는 방법에 내 부분에 무지 생각, 어쨌든이 내 구현 파일입니다생명의 해쉬 테이블 .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

std::find을 얻으려면 #include <algorithm>이 필요합니다. 이것은 아마도 find에 전화 할 때 사용하기를 원할 것입니다. 특히 헤더에 using namespace std을 사용하지 마십시오.

+0

이제 작동 중입니다. 감사합니다. – newwarrior21st