2017-04-07 16 views
0

私はC++の世界で初めてです。私は助けが必要です。私の問題は、私の構造ハッシュペアの配列を実装しようとしている、そこにキーとデータです。この構造体では、hasNextとnextというメソッドを持つネストされた構造イテレータがあります。私の配列(この配列は親である)は入れ子構造から見ることができないので、コンストラクタを渡す必要がありますが、エラーは ":can not convert ..."です。問題はgetIteratorメソッドのpass _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]; 

あなたが望むものはおそらくされていない、Par構造体へのポインタのサイズSizeの配列を宣言。

この配列をコンストラクタ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

@ JanMesarc別のオプションがあります: 'iterator'コンストラクタの宣言を' Par ** 'を受け入れるように変更し、' index'と 'pomPar'で同じ変更を行います。 – alexeykuzmin0

+0

ありがとう、これは素晴らしい作品です:) –

関連する問題