views:

44

answers:

2

I'm sure there's a better way to do this.

I'm trying to create a class HashTable that is given a size at instantiation, so it can't be given a size at design time, so I can't use an array as my internal representation of the table, as far as I know.

So here's what I'm trying to do:

#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
};

From what I've seen, vector is the best data structure to use here, because it will let me give it a size in the constructor, and lets me use myTable[index] style notation to access its contents. However, VS2010 doesn't like my use of vector<* TableNode<T>> and I can't say I blame it. So, how do I create an array or vector or whatever of pointers to TableNode<T> elements?

+2  A: 

Move the asterisk around:

vector<TableNode<T>*> myTable;

myTable = vector<TableNode<T>*>(size,NULL);
Donotalo
A: 
vector<TableNode<T>*> myTable;
myTable = vector<TableNode<T>*>(size,NULL);

FTFY.

Jacob