interstice
interstice

Reputation: 115

Visual C++ Linker Error 2019

I'm trying to include a simple hash table class in some files with a header class. But whenever I try to compile I get several errors like this:

LNK2019: unresolved external symbol " public: __thiscall HashTable::~HashTable(void)" (??1HashTable@@QAE@XZ) referenced in function _main "

I'm using Visual Studio 2010. I am aware that this means it can't find the function definition in any of the source files. But I have defined them, in a file in the same directory as the file it's called in. Perhaps Visual Studio doesn't look in the current directory unless you set some linker option?

Here is the source code:

//HashTable.h
#ifndef HASH_H
#define HASH_H

class HashTable {

public:
    HashTable();
    ~HashTable();
    void AddPair(char* address, int value);
    //Self explanatory
    int GetValue(char* address);
    //Also self-explanatory. If the value doesn't exist it throws "No such address"

};

#endif



//HashTable.cpp
class HashTable {
protected:
    int HighValue;
    char** AddressTable;
    int* Table;

public:
    HashTable(){
        HighValue = 0;
    }
    ~HashTable(){
        delete AddressTable;
        delete Table;
    }
    void AddPair(char* address, int value){
        AddressTable[HighValue] = address;
        Table[HighValue] = value;
        HighValue += 1;
    }
    int GetValue(char* address){
        for (int i = 0; i<HighValue; i++){
            if (AddressTable[HighValue] == address) {

                return Table[HighValue];
            }
        }
        //If the value doesn't exist throw an exception to the calling program
        throw 1;
    };

};

Upvotes: 0

Views: 1695

Answers (1)

Luchian Grigore
Luchian Grigore

Reputation: 258678

No you have not. You created a new class.

The proper way to define the methods is:

//HashTable.cpp

#include "HashTable.h"
HashTable::HashTable(){
    HighValue = 0;
}
HashTable::~HashTable(){
    delete AddressTable;
    delete Table;
}
void HashTable::AddPair(char* address, int value){
    AddressTable[HighValue] = address;
    Table[HighValue] = value;
    HighValue += 1;
}
int HashTable::GetValue(char* address){
    for (int i = 0; i<HighValue; i++){
        if (AddressTable[HighValue] == address) {

            return Table[HighValue];
        }
    }
    //If the value doesn't exist throw an exception to the calling program
    throw 1;
};

Upvotes: 1

Related Questions