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

标签 c++ visual-studio-2010

好吧,我正在处理一个 C++ 项目,但我只是想不出如何减轻这个链接器错误。下面是:

1>test4.obj : error LNK2019: unresolved external symbol "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 &)" (?insert@?$OrderedList@VRecord@@D@@UAEXABVRecord@@@Z)

如果有人可以帮助分解并向我翻译 Visual Studio 2010 所说的内容,那就太棒了(我真的很想更好地阅读输出)。我一直在阅读这个特定的错误,但我仍然不明白为什么它适用于我的代码。

编辑:binarySearch 方法正在 OrderedList.cpp 文件中实现。我还在包含我的主要文件的文件中使用#include“OrderedList.cpp”语句。

有问题的两个函数:

插入原型(prototype):

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);
    }
} 
}

二分搜索原型(prototype):

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 的链接器不算指定函数??!

关于c++ - 了解 Visual Studio 2010 中的此错误 (LNK 2019),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12827846/

相关文章:

c++ - 更正 size_t : %zu or %Iu? 的 printf 格式说明符

c++ - 可能的 header 冲突

c# - 创建一个独立的 .exe 文件

c++ - 禁止特定函数模板实例化

c++ - 如何在GPU内核中使用Eigen稀疏矩阵

c++ - 从特定代码部分跳转到函数开始

c++ - 在基于 Visual Studio MFC 的应用程序中禁用事件处理程序

c++ - 帮助解决错误 LNK2019 : unresolved external symbol _WinMain@16 referenced in function ___tmainCRTStartup

c++ - 运算符重载 - C++

C++ - 仅使用数组进行链排序