我試圖爲散列表構建一個構造函數,但失敗了。 任何人都可以告訴我構造函數代碼有什麼問題嗎?C++類中的矢量初始化失敗
#include <iostream>
#include <string>
#include <vector>
using namespace std;
struct node {
string s;
node* next;
};
class QueryProcessor{
int bucket_count;
vector<node*> elems;
explicit QueryProcessor(int bucket_count) : bucket_count(bucket_count), elems(bucket_count) {
for (int i = 0; i < bucket_count; ++i) {
elems[i]->s = " ";
elems[i]->next = NULL;
}
}
};
int main(){
int bucket_count;
cin >> bucket_count;
QueryProcessor proc(bucket_count);
}
'elems(bucket_count)',請注意vector中的所有'bucket_count'元素都是空指針。 – songyuanyao
未在您的代碼中看到任何錯誤。編譯器說了什麼? –
我已經明確了我的代碼。對於那個很抱歉。 –