2016-12-14 7 views
0

私はHashTableクラスをテンプレートにしてテンプレートを作成することにしましたが、問題が発生しました。私のHashTable<T>テンプレートの中にはitemsというBucketというデータメンバ配列があります。これはHashTable<T>クラス内の構造体です。 itemsを初期化した後、Bucketのメンバーにテンプレートのコードのうちのどこかにアクセスすることができません。テンプレート内の構造体メンバーにアクセスできない

構造体と変数の定義の前にtypenametemplate<class T>を入れてみましたが、動作させることができませんでした。ここで

'keyValue': undeclared identifier

#ifndef HASH_TABLE_ 
#define HASH_TABLE_ 

using namespace std; 
#include <iostream> 

template<class T> 
class HashTable 
{ 

public: 

HashTable(int numItems) { 
    if (numItems <= 0) { 
     throw std::invalid_argument("Invalid HashTable size"); 
    } 
    currItems = 0; 

    //B must be the next prime after 2 * numItems 
    B = 1000; 

    items = Bucket[B]; //allocate array of Buckets 

    items[0].keyVal; //ERROR: undeclared identifier 
} 

bool insert(T* newItem, int key) { 
    bool retVal = false; 

    if (currItems < B && newItem != NULL) { //cannot insert to full HashTable 
     int index = 0; 

     items[index].dataPtr = newItem; //ERROR:undeclared 
     items[index].keyVal = key;  //ERROR:undeclared 

     retVal = true; 
     currItems++; 
    } 

    return retVal; 
} 

private: 

struct Bucket { 
    T* dataPtr = NULL; 
    int keyVal = -1; 
}; 

Bucket * items; //array of buckets 
int B; //size of itemArray 
int currItems; //track number of items in HashTable 

}; 

#endif 

私にエラーを与えるのコードスニペットはなぜitems[x]items[x].keyValまたはitems[x].dataPtrが使用できないようにバケツに、アクセスしないのですか?私はitems = new Bucket[B]のような異なるタイプの初期化を試しましたが、どちらもうまくいきませんでしたので、テンプレート側にエラーがあると仮定しています。

私はどんな指針もありがとう!

+2

このコードは、nextPrimeおよびgetOpenBucketの実装が不足しているためコンパイルされません。また、アイテムを適切に割り当てることはできません(new演算子を使用するか、さらには生のポインタをスマートなものに変更してください)。ここにテンプレートの問題はありません –

+0

nextPrimeとgetOpenBucketを削除しました。新しい[]を使うと、私は現在持っているものの代わりに 'items = new Bucket [B]'を使うことを意味しますか?それは私に過去の同じエラーを与えているからです。私はC++ '98 Linuxマシンでコンパイルする必要があるので、スマートポインタを使用することはできません:( – shtuken

+0

テーブルを動的に作成したい場合は、メモリを割り当てる必要があります。あなたは新しいコンパイラを利用できるようになり、新しい標準を使用することができます。そして、このコードではC++ 11の機能を使用しています( "int keyVal = -1;")。 )私はあなたのコードを問題なくコンパイルしています。 –

答えて

0

使用する前にBucketを宣言する必要があります。

template<class T> 
class HashTable 
{ 
    struct Bucket { 
     T* dataPtr = NULL; 
     int keyVal = -1; 
    }; 


public: 

    HashTable(int numItems) { 
     if (numItems <= 0) { 
      throw std::invalid_argument("Invalid HashTable size"); 
     } 
     currItems = 0; 

     //B must be the next prime after 2 * numItems 
     B = nextPrime(numItems * 2); 

     items = new Bucket[B]; // <-- you forgot the 'new' 

     items[0].keyVal; //ERROR: undeclared identifier 
    } 

    bool insert(T* newItem, int key) { 
     bool retVal = false; 

     if (currItems < B && newItem != NULL) { //cannot insert to full HashTable 
      int index = getOpenBucket(key); 

      items[index].dataPtr = newItem; //ERROR:undeclared 
      items[index].keyVal = key;  //ERROR:undeclared 

      retVal = true; 
      currItems++; 
     } 

     return retVal; 
    } 

private: 


    Bucket * items; //array of buckets 
    int B; //size of itemArray 
    int currItems; //track number of items in HashTable 

}; 

ps。これをしないでください:using namespace std;がヘッダファイルにあります。

あなたのヘッダーを含むすべてのcppファイルのグローバルな名前空間を害するので、邪悪で反社会的です。誰もあなたの図書館を使用しないように保証する方法です。

+0

'hashtable.h(64):エラーC2065: 'keyValue':宣言されていない識別子'エラーです。これは、別のクラスの私のHashTableの宣言と関係がありますか? ? 'HashTable * customers = new HashTable (10000); '名前空間のヒントをありがとう、私はすぐに修正します! – shtuken

+0

'items = newバケット[B];'あ​​なたは 'new'を忘れました –

関連する問題