错误LNK2019,LNK2001,LNK1120

时间:2016-02-26 03:55:38

标签: c++

我正在为此程序使用Microsoft Visual Studio,并且它不断抛出以下错误:

->error LNK2019: unresolved external symbol "public: virtual int __thiscall unorderedArrayListType::min(void)" (?min@unorderedArrayListType@@UAEHXZ) referenced in function _main   C:\Users\Emma.Barrett\Desktop\School\c++\Programs\chapter12_exercise9\chapter12_exercise9\Source.obj    chapter12_exercise9

->error LNK2001: unresolved external symbol "public: virtual int __thiscall unorderedArrayListType::min(void)" (?min@unorderedArrayListType@@UAEHXZ)    C:\Users\Emma.Barrett\Desktop\School\c++\Programs\chapter12_exercise9\chapter12_exercise9\unorderedArrayListTypeImp.obj chapter12_exercise9

->error LNK1120: 1 unresolved externals C:\Users\Emma.Barrett\Desktop\School\c++\Programs\chapter12_exercise9\Debug\chapter12_exercise9.exe chapter12_exercise9

我的程序编译正常,直到我将min()函数添加到unorderedArrayListTypeImp.cpparrayListTypeImp.cpp。我对C ++比较陌生,所以它可能是一个愚蠢的修复,但我似乎无法弄明白。

以下是unorderedArrayListTypeImp.cpp

#include "unorderedArrayListType.h"
#include <string>

void unorderedArrayListType::insertAt(int location, int insertItem)
{
    if (location < 0 || location >= maxSize)
    {
        cout << "The position of the item to be inserted is out of range"
            << endl;
    }
    else if (length >= maxSize)
    {
        cout << "Cannot insert in a full list"
            << endl;
    }
    else
    {
        for (int i = length; i > location; i--)
        {
            list[i] = list[i - 1];
        }
        list[location] = insertItem;

        length++;
    }
}

void unorderedArrayListType::insertEnd(int insertItem)
{
    if (length >= maxSize)
    {
        cout << "Cannot insert in a full list." << endl;
    }
    else
    {
        list[length] = insertItem;
        length++;
    }
}

int unorderedArrayListType::seqSearch(int searchItem) const
{
    int loc;
    bool found = false;

    loc = 0;

    while (loc < length && !found)
    {
        if (list[loc] == searchItem)
        {
            found = true;
        }
        else
        {
            loc++;
        }
    }
    if (found)
    {
        return loc;
    }
    else
    {
        return -1;
    }
}

void unorderedArrayListType::remove(int removeItem)
{
    int loc;

    if (length == 0)
    {
        cout << "Cannot delete from an empty list." << endl;
    }
    else
    {
        loc = seqSearch(removeItem);
        if (loc != -1)
        {
            removeAt(loc);
        }
        else
        {
            cout << "The item to be deleted is not in the list."
                << endl;
        }
    }
}

void unorderedArrayListType::replaceAt(int location, int repItem)
{
    if (location < 0 || location >= length)
    {
        cout << "The location of the item to be replaced is out of range."
            << endl;
    }
    else
    {
        list[location] = repItem;
    }
}

int min()
{
    arrayListType:min();
    return 0;
}

unorderedArrayListType::unorderedArrayListType(int size)
    :arrayListType(size)
{

}

以下是arrayListTypeImp.cpp

#include "arrayListType.h"
#include <iostream>
#include <string>
using namespace std;

bool arrayListType::isEmpty() const
{
    return (length == 0);
}

bool arrayListType::isFull() const
{
    return (length == maxSize);
}

int arrayListType::listSize() const
{
    return length;
}

int arrayListType::maxListSize() const
{
    return maxSize;
}
void arrayListType::print() const
{
    for (int i = 0; i < length; i++)
    {
        cout << list[i] << " ";
    }
    cout << endl;
}

bool arrayListType::isItemAtEqual(int location, int item) const
{
    if (location < 0 || location >= length)
    {
        cout << "The location of the item to be removed is out of range."
            << endl;
        return false;
    }
    else
        return (list[location] == item);
}

void arrayListType::removeAt(int location)
{
    if (location < 0 || location >= length)
    {
        cout << "The location of the item to be removed is out of range"
            << endl;
    }
    else
    {
        for (int i = location; i < length - 1; i++)
        {
            list[i] = list[i + 1];
        }
        length--;
    }
}

void arrayListType::retrieveAt(int location, int& retItem) const
{
    if (location < 0 || location >= length)
    {
        cout << "The location of the item to be retrieved is out of range"
            << endl;
    }
    else
    {
        retItem = list[location];
    }
}

void arrayListType::clearList()
{
    length = 0;
}

int arrayListType::min()
{
    int smallest = list[0];
    for (int i = 0; i < length; i++)
    {
        if (list[i] < smallest)
        {
            return i;
        }
    }
}

arrayListType::arrayListType(int size)
{
    if (size <= 0)
    {
        cout << "The array size must be positive. Creating an array of the size 100."
            << endl;
        maxSize = 100;
    }
    else
    {
        maxSize = size;
    }
    length = 0;
    list = new int[maxSize];
}

arrayListType::~arrayListType()
{
    delete[] list;
}

arrayListType::arrayListType(const arrayListType& otherList)
{
    maxSize = otherList.maxSize;
    length = otherList.length;

    list = new int[maxSize]; //create the array

    for (int j = 0; j < length; j++)
    {
        list[j] = otherList.list[j];
    }
}

这是源文件:

#include <iostream>
#include "arrayListType.h"
#include "unorderedArrayListType.h"

using namespace std;

int main()
{
    unorderedArrayListType intList(25);

    int number;

    cout << "Enter 8 integers: ";

    for (int count = 0; count < 8; count++)
    {
        cin >> number;
        intList.insertEnd(number);
    }

    cout << endl;
    cout << "intList: ";
    intList.print();
    cout << endl;

    cout << "The smallest number in intList: "
        << intList.min() << endl;
    system("pause");
    return 0;
}

1 个答案:

答案 0 :(得分:0)

您忘记了.cpp中unorderedArrayListType::的{​​{1}}。

min

没有它,您可以创建一个新的非成员函数,而不是定义函数。然后,链接器尝试查找标头中声明的成员函数int unorderedArrayListType::min() 的实现,但不能。