2010-10-07 150 views
1

我相信有更好的方法來做到這一點。創建一個指向抽象對象的指針向量

我想創建一個在實例化時給定大小的類HashTable,所以無法在設計時給出大小,所以我不能使用數組作爲表的內部表示,我所知道的。

因此,這裏是我想要做的事:

#include <vector> 
#include <iostream> 
#include "Nodes.h" 

using namespace std; 

template <class T> 
class HashTable{ 

protected: 
    vector<* TableNode<T>> myTable; 
    //other protected methods go here 

public: 
    HashTable(int size){ 
     myTable = vector<* TableNode<T>>(size,NULL); 
    } 

//The rest of the class 
}; 

從我所看到的,向量是最好的數據結構在這裏使用,因爲它可以讓我給它一個尺寸在構造,並讓我使用myTable[index]風格表示法來訪問其內容。但是,VS2010不喜歡我使用vector<* TableNode<T>>,我不能說我責怪它。那麼,我該如何創建一個數組或向量或任何指向TableNode<T>元素的指針呢?

+0

你得到了什麼錯誤 – rerun 2010-10-07 17:04:26

回答

2

四處移動星號:

vector<TableNode<T>*> myTable; 

myTable = vector<TableNode<T>*>(size,NULL); 
0
vector<TableNode<T>*> myTable; 
myTable = vector<TableNode<T>*>(size,NULL); 

FTFY。