Hey, I have a hash table for primitive values that uses a template to store the key Type and value Type, everything compiles fine as far as that's concerned, but i have a wrapper class that associates floats with ints (array indices) and that's all good except for one syntactic issue.

In my wrapper class header I declare a pointer to a primHashTable like primHashTable<float, int> *pht; and then in my constructor I try to initialized/instantiate (never was a vocab whiz) the pointer like this pht = new primHashTable<float, int>(); which naturally fails epically.

I'm pretty ok at C and Java but my C++ knowledge is pretty much a bastardization of my knowledge of each of those languages. I was wondering if anyone knew a way to make this work before I bash my brains in, or just do it the easy way and just typedef the types in the primHashTable header (kind of inelegant)

Any help is much appreciated, thanks

Alex

Maybe you should try leaving out the parenthesis?

Also, why aren't you posting the error? You should at least do that if you want people on here to help X_X

indexTable.cpp: In constructor âindexTable::indexTable()â:
indexTable.cpp:5: error: expected type-specifier before âprimHashTableâ
indexTable.cpp:5: error: cannot convert âint*â to âprimHashTable<float, int>*â in assignment
indexTable.cpp:5: error: expected `;' before âprimHashTableâ

For some reason, Putty screws up "s pretty good

and when I dropped the ()'s, I got linking errors (as I did when I dropped the <float, int>)

Can you post the rest of your code? I have a feeling it has something to do with the way you've created your primHashTable class.

primHashTable.h

template <class keyType, class valType>
class primHashTable
{
    private:
        struct pair_t;
        typedef struct pair_t
        {
            keyType key;
            valType val;
            struct pair_t *next;
        } pair;
        pair **table;
        int count;
        int maxCount;
        int size;
        int curPrime;
        void growTable();
    public:
        primHashTable();
        ~primHashTable();
        int containsKey(keyType);
        int add(keyType, valType);
        int get(keyType, valType *);
        int remove(keyType);
        int getCount();
};

primHashTable.cpp

template <class keyType, class valType>
primHashTable<keyType, valType>::primHashTable()
{
    curPrime = 0;
    count = 0;
    size = hashPrimes[curPrime];
    maxCount = (int) (MAX_LOAD_FACTOR * size);
    table = (pair **) malloc(sizeof(pair *) * size);
}

template <class keyType, class valType>
primHashTable<keyType, valType>::~primHashTable()
{
    pair *p;
    int i;
    for(i = 0; i < size; i++)
    {
        while((p = table[i]))
        {
            table[i] = table[i]->next;
            free(p);
        }
    }

    free(table);
}

template <class keyType, class valType>
int primHashTable<keyType, valType>::containsKey(keyType key)
{
    pair *p = table[HASH(key, size)];

    while(p)
    {
        if(p->key == key)
            return 1;
        p = p->next;
    }

    return 0;
}

template <class keyType, class valType>
int primHashTable<keyType, valType>::get(keyType key, valType *val)
{
    pair *p = table[HASH(key, size)];

    while(p)
    {
        if(p->key == key)
        {
            *val = p->val;
            return 0;
        }
        p = p->next;
    }

    return 1;
}

template <class keyType, class valType>
int primHashTable<keyType, valType>::add(keyType key, valType val)
{
    pair *p;
    int hashVal = HASH(key, size);

    p = table[hashVal];

    while(p)
    {
        if(p->key == key)
        {
            p->val = val;
            return 0;
        }

        p = p->next;
    }

    if(++count > maxCount)
    {
        growTable();
        hashVal = HASH(key, size);
    }

    p = (pair *) malloc(sizeof(pair));
    p->key = key;
    p->val = val;
    p->next = table[hashVal];
    table[hashVal] = p;

    return 0;
}

template <class keyType, class valType>
int primHashTable<keyType, valType>::remove(keyType key)
{
    int hashVal = HASH(key, size);
    pair *t, *p = table[hashVal];

    if(p->key == key)
    {
        table[hashVal] = p->next;
        free(p);
        return 0;
    }

    while(p->next)
    {
        if(p->next->key == key)
        {
            t = p->next;
            p->next = t->next;
            free(t);
            return 0;
        }

        p = p->next;
    }

    return 1;
}

template <class keyType, class valType>
int primHashTable<keyType, valType>::getCount()
{
    return count;
}

template <class keyType, class valType>
void primHashTable<keyType, valType>::growTable()
{
    int newSize;
    pair **newTable;
    pair *p;
    int hashVal;
    int i;

    /*establish the new size */
    if(++curPrime >= NUM_PRIMES)
        newSize = (size * 2) + 1;
    else
        newSize = hashPrimes[curPrime];

    /* malloc and initialized the new table */
    newTable = (pair **) malloc(sizeof(pair *) * newSize);

    for(i = 0; i < newSize; i++)
        newTable[i] = NULL;


    for(i = 0; i < size; i++)
    {
        while((p = table[i]))
        {
            table[i] = table[i]->next;
            hashVal = HASH(p->key, newSize);
            p->next = newTable[hashVal];
            newTable[hashVal] = p;
        }
    }

    free(table);

    table = newTable;
    size = newSize;
    maxCount = (int) (MAX_LOAD_FACTOR * newSize);
}

Do to mysterious linker stuff, you have to include the body of the C++ code inside the code that calls it.

so a #include "primHashTable.cpp" did it

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.