2012-03-28 80 views
0

我想在包含头类的某些文件中包含一个简单的哈希表类。但是每当我尝试编译时,我会得到如下几个错误:Visual C++链接器错误2019

LNK2019:无法解析的外部符号“public:__thiscall HashTable ::〜HashTable(void)”(?? 1HashTable @@ QAE @ XZ)

我正在使用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

[如何创建在C++类(http://www.learn-programming.za.net/programming_cpp_learn10.html) – 2012-03-28 21:03:07

回答

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; 
}; 
+0

阿。所以当定义函数时,我应该使用HashTable :: function()? – user1296991 2012-03-28 20:41:45

+0

@ user1296991。 – 2012-03-28 20:42:09

+0

我会在头文件中包含任何私有变量,对吧? – user1296991 2012-03-28 20:44:46