0

Hey guys so I am working on a Hash program in C++ for my class. I am using a template T class for my header file and when I try to call the class constructor from main it gives me a undeclared identifier and type 'int' unexpected error. Here is my HashTable.h file:

#pragma once
#include "Record.h"
#define MAXHASH 1000
#ifndef HASHTABLE_H
#define HASHTABLE_H
using namespace std;

template <class T> class HashTable
{
public:
    HashTable();
    ~HashTable();
    bool insert(int, T, int&);
    bool remove(int);
    bool find(int, T&);
    float alpha();
private:
    int key;
    T value;
 };

#endif

and here is my main:

#include "HashTable.h"
#include "stdafx.h"
#include <string>
#include <iostream>
using namespace std;

int main()
{
    HashTable<int> *test = new HashTable<int>();
    return 0;
}

Here's the constructor in the .cpp file as well:

#pragma once
#include "stdafx.h"
#include "HashTable.h"
#include "Record.h"
#define HASHTABLE_CPP
#ifndef HASTABLE_CPP


template <class T>
HashTable<T>::HashTable()
{
    Record hashArray = new Record[MAXHASH];
    for (int i = 0; i < MAXHASH; i++)
    {
        hashArray[i]->key = 0;
        hashArray[i]->value = NULL;
    }
}

The specific errors I am getting are:

Error C2062   type 'int' unexpected identifier 

Error C2065   'HashTable': undeclared identifier      

Both the errors point to the call line in main.

It is difficult because I can't test my program until I can get this test hash to work. Any input on how to fix this issue would be awesome!

BusyProgrammer
  • 2,783
  • 5
  • 18
  • 31
  • 2
    use `typename T` – Hatted Rooster Apr 18 '17 at 20:14
  • 2
    Not a dupe of [this](http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file) yet but you will run into it once you get it to compile. – NathanOliver Apr 18 '17 at 20:14
  • 2
    Off topic regarding: `HashTable *test = new HashTable();` Don't new unless you have to. `HashTable test;` will most likely do what you want. Also watch out for `Record hashArray = new Record[MAXHASH];` `hashArray ` is a local variable, so you will lose track of where the allocated storage is once the function exits. Consider making it a member variable and, as before, avoid `new`ing it. – user4581301 Apr 18 '17 at 20:27
  • 2
    The first thing to do when you get mysterious compile-time errors with Visual Studio is to remove `#include "stdafx.h"`; it's a Microsoft thing, and it's supposed to be just a compile-time optimization, but it often screws things up. (Also, it should be the **first** `#include` directive if you use it) – Pete Becker Apr 18 '17 at 20:31
  • I feel pretty stupid but it was because the #include "stdafx.h" was not first. haha Thanks so much guys! – Andrew Vodicka Apr 18 '17 at 20:35

1 Answers1

0

the old Microsoft application file extensions interface "stdafx.h" has to be the first directive listed if the pre-compiled header is used. It actually works best because VS expects it...I always use it.

Dr t
  • 247
  • 4
  • 11