私はプライベートメンバ変数として文字列ワードとint頻度を格納するWordFrequencyというクラスを作成しました。 ハッシュテーブルにWordFrequency **、hashsize、currentitemsを含むHashTable関数。コピーコンストラクタとゲッター(ミューテータ関数)のエラー
私は常にエラー取得していますコピーコンストラクタを使用している間 - スレッド1:EXC_BAD_ACCESS(コード= 1、アドレス= 0x0の) コピーコンストラクタで呼び出されたときWordFrequencyクラスのgetter関数に私をリダイレクトします。 なぜそれが起こっているのか理解できません。
コピーコンストラクタ。
Hashtable:: Hashtable(const Hashtable &hash){
WordFrequency** temp = new WordFrequency* [hash.hashSize];
this->arr = temp;
for (int i = 0 ; i < hash.hashSize ; i++) {
if (hash.arr[i] == NULL){ //pointer in hashstable is null
continue;
}
//bucket is not empty
if(this->search(this->arr[i]->getWord()) != 0 ){ //if same string already found in this hashtable
this->arr[i]->increment(); // incrtement the frequency
continue;
}
//else the string doest even exist in the hashtable.
WordFrequency x ((hash.arr[i])->getWord()); //deep copying the word from the parameter hash
temp[i] = &x; //pointing the hash table to the new the object
}
this->hashSize = hash.hashSize;
this->currentItems = hash.currentItems;
}
wordfrequencyクラスのgetter関数。
string WordFrequency:: getWord() const {
return this->word;
}
ゲッター機能は非常にシンプルなようですが、なぜこのエラーが発生するのか分かりません。
私はまた、問題になるかもしれない自分のデストラクターの原因も含めています。
Hashtable:: ~Hashtable() {
for (int i = 0 ; i < this->hashSize ; i++){
delete this->arr[i];
}
delete [] this->arr;
this->hashSize = 0;
this->currentItems = 0;
}
出力オペレータ - ハッシュテーブルコンストラクタのループのために、あなたはスタック割り当てられたオブジェクトのポインタを格納しているで
ostream& operator<< (ostream &out, const Hashtable &h){
out << "Hashtable with size - " << h.hashSize << "and no of elements - " << h.currentItems << endl;
for (int i = 0 ; i < h.hashSize ; i++){
if (h.arr[i] == NULL){
out << "0";
continue;
}
else {
out << ((h.arr[i])->getWord()); //bad access
}
}
out << endl;
return out;
}
std :: vectorを使用してください。 –
_ "私はいつもエラーが発生しています - スレッド1:EXC_BAD_ACCESS" _ OK、あなたはデバッガを実行させ、そのエラーにつながる正確なスタックトレースを関数引数の値とともに見ることができますあなたのアクセスが悪い理由を理解することができます。 –
私は割り当てによってカスタムハッシュテーブルを使用する必要があると思いますか?そうでなければ 'std :: map'や' std :: unordered_map'で多くの問題を解決することができます。 – user4581301