我试图在一些带有标题类的文件中包含一个简单的哈希表类。但每当我尝试编译时,我会得到几个这样的错误:
LNK2019:未解决的外部符号" public:__ thishisall HashTable :: ~HashTable(void)" (?? 1HashTable @@ QAE @ XZ)在函数_main"
中引用我正在使用Visual Studio 2010.我知道这意味着它无法在任何源文件中找到函数定义。但是我已经将它们定义在与它所调用的文件相同的目录中的文件中。除非你设置了一些链接器选项,否则Visual Studio可能不会查看当前目录?
以下是源代码:
//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;
};
};
答案 0 :(得分:1)
不,你没有。您创建了一个新的class
。
定义方法的正确方法是:
//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;
};