好的,所以我正在开发一个 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;
}
此外,记录类:
class Record
{
public:
Record () { key = char(0); }
void setKey(char newKey) { key = newKey; }
char getKey() const { return key; }
private:
char key;
};