2016-02-26 107 views
-2

我使用Microsoft Visual Studio對這一計劃,並不斷拋出以下錯誤:錯誤LNK2019,LNK2001,LNK1120

->error LNK2019: unresolved external symbol "public: virtual int __thiscall unorderedArrayListType::min(void)" ([email protected]@@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)" ([email protected]@@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; 
} 

回答

0

你忘了unorderedArrayListType::min。 CPP。

int unorderedArrayListType::min() 

沒有它,而不是定義函數,而是創建一個新的非成員函數。鏈接器然後嘗試查找在頭中聲明的成員函數min的實現,但不能。

+0

太棒了。哈哈,我現在真的很愚蠢。 –

相關問題