2012-03-28 98 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