【发布时间】:2012-10-10 20:31:11
【问题描述】:
好吧,我正在处理一个 C++ 项目,但我不知道如何缓解这个链接器错误。如下:
1>test4.obj:错误 LNK2019:引用了无法解析的外部符号“private: bool __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@@UAEXABVRecord@@@Z)
如果有人可以帮助分解并将 Visual Studio 2010 所说的内容翻译给我,那就太棒了(我真的想更好地阅读输出)。我一直在阅读这个特定错误,但我仍然不明白为什么它会应用于我的代码。
编辑:binarySearch 方法正在 OrderedList.cpp 文件中实现。我还在包含我的主文件的文件中使用#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;
}
另外,Record 类:
class Record
{
public:
Record () { key = char(0); }
void setKey(char newKey) { key = newKey; }
char getKey() const { return key; }
private:
char key;
};
【问题讨论】:
-
你的函数模板中
DataType在哪里使用? -
DataType 是 KeyType 在列表中跟踪的内容。关键是保持数据成员有序。就像 account amount 是一个 float DataType 一样,account number 是 int KeyType。