了解Visual Studio 2010中的此错误(LNK 2019)

Understanding this error (LNK 2019) in Visual Studio 2010

本文关键字:错误 LNK 2019 Visual Studio 2010 了解      更新时间:2023-10-16

好吧,我正在做一个C++项目,我只是不知道如何减轻这个链接器错误。下面是:

1>test4.obj:错误LNK2019:未解析的外部符号"private:boll__thiscall OrderedList::binarySearch(char,int&)"(?binarySearch@$OrderedList@VRecord@@D@@AAE_NDAAH@Z)在函数"public:virtual void __thiscall OrderedList::insert(class Record const&)"中引用(?insert@$OrderedList@VRecord@@D@@UAEXABV记录@@@Z)

如果有人能帮我分解并翻译Visual Studio 2010所说的内容,那将是令人惊叹的(我真的想更好地阅读输出)。我一直在阅读这个特定的错误,但我仍然不明白为什么它会应用于我的代码。

EDIT:binarySearch方法正在OrderedList.cpp文件中实现。我还在包含main的文件中使用#include"OrderedList.cpp"语句。

有问题的两个功能:

插入原型:

virtual void insert ( const DataType &newDataItem ) throw ( logic_error );

插入:

template <typename DataType, typename KeyType>
void OrderedList<DataType, KeyType>::insert(const DataType &newDataItem) 
throw (logic_error ) {
int index = 0;
if (size == maxSize) {
throw logic_error("List is full.");
} 
else { 
KeyType searchKey = newDataItem.getKey();
if (binarySearch(searchKey, index)) {
cursor = index;
dataItems[cursor] = newDataItem;
}
else {
cursor = index;
insert(newDataItem);
}
} 
}

二进制搜索原型:

bool binarySearch ( KeyType searchKey, int &index );

二进制搜索:

template < typename DataType, typename KeyType >
bool binarySearch (KeyType searchKey, int &index ) {
int low  = 0;        // Low index of current search range
int high = size - 1;    // High index of current search range
bool result;            // Result returned
while ( low <= high )
{
index = ( low + high ) / 2;               // Compute midpoint
if ( searchKey < dataItems[index].getKey() )
high = index - 1;                      // Search lower half
else if ( searchKey > dataItems[index].getKey() )
low = index + 1;                       // Search upper half
else
break;                                 // searchKey found
}
if ( low <= high )
result = true;       // searchKey found
else
{
index = high;        // searchKey not found, adjust index
result = false;
}
return result;
}

此外,记录类:

class Record
{
public: 
Record () { key = char(0); }
void setKey(char newKey) { key = newKey; }
char getKey() const { return key; }
private:
char key;

};

是否可能行:

template < typename DataType, typename KeyType >
bool binarySearch (KeyType searchKey, int &index )

在您的cpp文件中,而您只是忘记将其实现为:

template < typename DataType, typename KeyType >
bool OrderedList<DataType, KeyType>::binarySearch(KeyType searchKey, int &index)

那么binarySearch只是一个全局函数,而不是来自OrderedList的函数,并且试图查找OrderedList<DataType, KeyType>::binarySearch的链接器不将其算作指定函数??!

只有使用正确的模板参数调用模板函数,它才算真正的函数。在这种情况下,由于OrderedList.cpp文件不包含对函数的调用,因此从未生成实际的函数代码。这就是链接器找不到它的原因。

通常在头文件中定义模板函数或类来避免这个问题。

让我们逐行分解:

1>test4.obj : error LNK2019: unresolved external symbol 

这告诉您,在test4.obj文件中,编译器无法找到预期可用的已编译对象。

"private: bool __thiscall OrderedList::binarySearch(char,int &)" 

这是它找不到的对象(在本例中是成员函数)的函数签名。

(?binarySearch@?    $OrderedList@VRecord@@D@@AAE_NDAAH@Z) 

这是上面函数的"损坏名称"——编译器在对象文件中给出的名称。它为编译器提供了一种文本方式来验证名称相似但类型关联不同的对象。

referenced in function 

这是一行,告诉您下一个对象将是引用未解析符号的位置。

"public: virtual void __thiscall OrderedList::insert(class Record const &)"

这是调用导致错误的符号的对象的函数签名。注意,模板参数在这里不可见,但任何类型绑定的参数都可见,所以您知道DataTypeRecord类型,但不知道KeyType是什么。

(?insert@?$OrderedList@VRecord@@D@@UAEXABVRecord@@@Z)

这是上面函数的错误名称。


现在,让我们看看这意味着什么。您有一个模板方法,它调用的似乎是全局函数模板。该全局函数模板有两个模板参数,其中只有一个由函数调用提供。

换句话说,您没有提供DataType,所以编译器不知道如何生成函数模板binarySearch的模板专用化。

这里的好消息是,你实际上并不需要DataType模板参数,所以你应该简单地消除它。到那时,你的函数模板将完全专业化,应该进行编译。