【问题标题】:Visual C++ Linker Error 2019Visual C++ 链接器错误 2019
【发布时间】:2012-04-12 12:05:31
【问题描述】:

我正在尝试在一些带有标头类的文件中包含一个简单的哈希表类。但是每当我尝试编译时,我都会遇到几个这样的错误:

LNK2019:函数 _main 中引用的未解析的外部符号“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;
    };

};

【问题讨论】:

标签: c++ visual-studio-2010 linker


【解决方案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;
};

【讨论】:

  • 啊。所以在定义函数时我应该使用 HashTable::function()?
  • 我会在头文件中包含任何私有变量,对吧?
  • @user1296991 是的,您在类定义中声明数据成员。类定义是头文件中的部分:class HashTable {....};.
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-17
  • 2020-09-19
  • 2013-12-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多