2017-04-07 53 views
0

我是C++世界的新手,我需要幫助。我的問題是我嘗試實現我的結構散列對數組,有關鍵和數據。在這個結構中,我使用方法hasNext和next嵌套了結構迭代器。因爲我不能從嵌套結構中看到我的數組(這個數組在父類中),所以我需要通過構造函數傳遞它,但是出現錯誤「:can not convert from ...」,問題是在方法getIterator中傳遞_array。代碼如下。你可以幫幫我嗎?由於帶有hasNext和Next的C++迭代器

#pragma once 
template<typename T, typename U, int Size, int(*HashFunction)(T)> 
struct HashPairPole { 

// Pair - key - data 
struct Par { 
    // key 
    T _first; 
    // data 
    U _second; 
    // list for collision records 
    Par* _overflow; 

    Par(T t, U u) { 
     _first = t; 
     _second = u; 
     _overflow = nullptr; 
    } 
}; 


HashParovePole() {} 

// Static array for save data 
Par* _array[Size]; 

// Add record into hash table 
void add(T t, U u) { 
    // calculating of index  
    Par* prvek; 
    int idx = HashFunction(t) % Size; 

    // Element will be saved in _array[idx], if it is free, else will be 
    //saved to list (->_overflow) 
    prvek = new Par(t, u); 

    if (_array[idx] == nullptr) { 
     _array[idx] = prvek; 
    } 
    else { 
     prvek->_overflow = _array[idx]; 
    } 
    _array[idx] = prvek; 
} 

// Get data from hash tabule 
U& get(T t) { 
    int idx = HashFunction(t) % Size; 
    Par * prvni = _array[idx]; 

    while (prvni->_overflow != nullptr) { 
     if (prvni->_first == t) { 
      return prvni->_second; 
     } 
     prvni = prvni->_overflow; 
    } 

} 

U& operator[](T t) { 
    return get(t); 
} 

U operator[](T t) const { 
    const U temp = get(t); 
    return temp; 
} 

// Iterator for walking all hash table 
struct iterator { 
    Par* index[Size]; 
    Par* pomPar; 
    int temp = 0; 

    iterator(Par * _array) { 
     index = _array; 
     pomPar = index[0]; 
    } 

    bool hasNext()const { 
     return pomPar != nullptr; 
    } 


    std::pair<T, U> next() { 
     std::pair<T, U> data; 
     if (hasNext()) { 
      data.first = pomPar->_first; 
      data.second = pomPar->_second; 
      pomPar = pomPar->_overflow; 
     } 
     temp++; 
     pomPar = index[temp]; 
     return data; 
    } 
}; 

    // Vytvori iterator 
    iterator getIterator() { 
     return iterator(_array); 
    } 

}; 
+0

在哪行中有彙編錯誤?什麼是編譯錯誤的文本?請閱讀FAQ併發布MCVE:http://stackoverflow.com/help/mcve – alexeykuzmin0

+1

'Par * _array!= Par * index [Size]'。 – NathanOliver

+1

關閉主題,但...我猜'HashParovePole(){}'應該是'HashPairPole(){}'? –

回答

0

據我看到的,問題是在這一行:

Par* _array[Size]; 

在這裏,您聲明指向的大小Size的數組Par結構,這可能不是你想要的。

稍後嘗試將此數組傳遞給構造函數iterator(Par * _array),該構造函數接受指向Par結構的指針,這是不可能的。

我會以下列方式解決這個代碼:

Par _array[Size]; // Instead of Par* _array[Size] 
        // You need an array of structures instead of array of pointers 
... 
Par* index; // Instead of Par* index[Size] 
      // Here looks like index is a pointer to a current element 
... 
pomPar = index; // Instead of pomPar = index[0]; 
       // This is a pointer to the node, while index[0] is its value 

而且,考慮使用std::vector而不是原始指針。它會爲你處理內存管理問題。

+0

我無法將Par * _array更改爲Par _array,因爲我必須遵守教師的任務。我需要用這個迭代器來實現這個容器,因爲來自老師的源代碼使用這種方法,不幸的是我不能改變我在學校裏得到的源代碼。 –

+1

@JanMesarčOK,還有另一種選擇:改變'iterator'構造函數的聲明接受'帕**'和'與和index''pomPar'做同樣的改變。 – alexeykuzmin0

+0

謝謝你,這是偉大的工作:) –