链接2019错误 - 未解析的外部符号

时间:2017-02-18 19:13:17

标签: c++ class compiler-errors unresolved-external

我试过阅读这篇文章,但我并没有真正理解它:

What is an undefined reference/unresolved external symbol error and how do I fix it?

我直接从书中复制了这个程序,无法让它工作。我想在开始添加作业之前确保它有效。 有人可以告诉我为什么我会收到这些错误。

LNK2019 unresolved external symbol "public: void __thiscall 
arrayListType<int>::print(void)const " (?print@?$arrayListType@H@@QBEXXZ) 
referenced in function _main

LNK2019 unresolved external symbol "public: void __thiscall 
arrayListType<int>::insertAt(int,int const &)" (?insertAt@?
$arrayListType@H@@QAEXHABH@Z) referenced in function _main

LNK2019 unresolved external symbol "public: void __thiscall 
arrayListType<int>::remove(int const &)" (?remove@?
$arrayListType@H@@QAEXABH@Z) referenced in function _main

main.cpp中:

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

int main() {
    arrayListType<int> intList(100);
    arrayListType<string> stringList;

    int number;

    cout << "List 10: Enter 5 integers: ";

    for (int counter = 0; counter < 5; counter++) {
        cin >> number;
        intList.insertAt(counter, number);
    }

    cout << endl;
    cout << "List 19: The list you entered is: ";
    intList.print();
    cout << endl;

    cout << "List 20: Enter the item to be deleted: ";
    cin >> number;
    intList.remove(number);
    cout << "Line 23: After removing " << number << ", the list is" << endl;
    intList.print();
    cout << endl;

    string str;

    cout << "Line 27: Enter 5 strings: ";

    for (int counter = 0; counter < 5; counter++) {
        cin >> str;
        stringList.insertAt(counter, str);
    }

    cout << endl;
    cout << "Line 34: The list you enterd is: " << endl;
    stringList.print();
    cout << endl;

    cout << endl;
    cout << "Line 37: Enter the string you want to be deleted: " << endl;
    cin >> str;
    stringList.remove(str);
    cout << "Line 40: After removing " << str << ", the list is: " << endl;
    stringList.print();
    cout << endl;

    return 0;
}

arrayListType.h:

#pragma once
#ifndef ARRAY_LIST_TYPE_H
#define ARRAY_LIST_TYPE_H
template <class elemType>

class arrayListType {
public:
    const arrayListType<elemType>& operator = (const arrayListType<elemType>&);
    bool isEmpty() const;
    bool isFull() const;
    int listSize() const;
    int masListSize() const;
    void print() const;
    bool isItemEqual(int location, const elemType& item) const;
    void insertAt(int location, const elemType& insertItem);
    void insertEnd(const elemType& insertItem);
    void retrieveAt(int location, elemType& retItem) const;
    void removeAt(int location);
    void replaceAt(int location, const elemType& repItem);
    void clearList();
    int seqSearch(const elemType& item) const;
    void insert(const elemType& insertItem);
    void remove(const elemType& removeItem);
    arrayListType(int size = 100);
    arrayListType(const arrayListType<elemType>& otherList);
    ~arrayListType();
    int min();
protected:
    elemType *list;
    int length;
    int maxSize;
};


#endif

arrayListType.cpp:

//implementation file
//arrayListType.cpp
#include "arrayListType.h"

//copy constructor
template <class elemType>
arrayListType<elemType>::arrayListType(const arrayListType<elemType>& otherList) {
    maxSize = otherList.maxSize;
    length = otherList.length;
    list = new elemType[maxSize];
    asser(list != null);

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

//overloading the assignmnet operator
template <class elemType>
const arrayListType<elemType>& arrayListType<elemType>::operator=(const arrayListType<elemType>& otherList) {
    if (this != &otherList) {
        delete[] list;
        maxSize = otherList.maxSize;
        length = otherList.length;
        list = new elemType[maxSize];
        assert(list != NULL);

        for (int i = 0; i < length; i++) {
            list[i] = otherList.list[i];
        }
    }
    return *this;
}

template <class elemType>
bool arrayListType<elemType>::isEmpty() const {
    return (length == 0);
}

template <class elemType>
bool arrayListType<elemType>::isFull() const {
    return (length == maxSize);
}

template <class elemType>
int arrayListType<elemType>::listSize() const {
    return length;
}

template <class elemType>
int arrayListType<elemType>::masListSize() const {
    return maxSize;
}

template <class elemType>
void arrayListType<elemType>::print() const {
    for (int i = 0; i < length; i++) {
        cout << list[i] << " ";
    }
    cout << endl;
}

template <class elemType>
bool arrayListType<elemType>::isItemEqual(int location, const elemType& item) const {
    return(list[location] == item);
}

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

template <class elemType>
void arrayListType<elemType>::insertEnd(const elemType& insertItem) {
    if (length >= maxSize) {
        cerr << "Cannot insert in a full list." << endl;
    }
    else {
        list[length] = insertItem;
        length++;
    }
}

template <class elemType>
void arrayListType<elemType>::removeAt(int location) {
    if (location < 0 || location >= length) {
        cerr << "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--;
        }
    }
}

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

template <class elemType>
void arrayListType<elemType>::replaceAt(int location, const elemType& repItem) {
    if (locatoin < 0 || location >= length){
        cerr << "The location of the item to be replaced is out of range." << endl;
    }
    else {
        list[location] = repItem;
    }
}

template <class elemType>
void arrayListType<elemType>::clearList() {
    length = 0;
}

template <class elemType>
int arrayListType<elemType>::seqSearch(const elemType& item) const {
    int loc;
    bool found = false;

    for (loc = 0; loc < length; loc++) {
        if (list[loc] == item) {
            found = true;
            break;
        }
        if (found) {
            return loc;
        }
        else {
            return -1;
        }
    }
}

template <class elemType>
void arrayListType<elemType>::insert(const elemType& insertItem) {
    int loc;
    if (length == 0) {
        list[length++] = insertItem;
    }
    else if (length == maxSize) {
        cerr << "Cannot inser in a full list." << endl;
    }
    else {
        loc = seqSearch(insertItem);
        if (loc == -1) {
            list[length++] = insertItem;
        }
        else {
            cerr << "The item to be inserted is already in the list. "
                << "No duplicates are allowed." << endl;
        }
    }
}

template <class elemType>
void arrayListType<elemType>::remove(const elemType& removeItem) {
    int loc;
    if (length == 0) {
        cerr << "Cannot delete from an empty list." << endl;
    }
    else {
        loc = seqSearch(removeItem);
        if (loc != -1) {
            removeAt(loc);
        }
        else {
            cerr << "The item to be deleted is not in the list." << endl;
        }
    }
}

template <class elemType>
arrayListType<elemType>::arrayListType(int size) {
    if (size < 0) {
        cerr << "The array size must be positive. Creating an array of size 100." << endl;
        maxSize = 100;
    }
    else {
        maxSize = size;
    }
    length = 0;
    list = new elemType[maxSize];
    assert(list != NULL);
}

//template <class elemType>
//arrayListType<elemType>::arrayListType(const arrayListType<elemType>& otherList) {
//  
//}

template <class elemType>
arrayListType<elemType>::~arrayListType() {
    delete[] list;
}

template <class elemType>
int arrayListType<elemType>::min() {

}

0 个答案:

没有答案