链接器错误与类模板

时间:2017-02-20 11:31:36

标签: c++ templates inheritance linker

我正在测试ADT链表实现。这是我上一学期的大学文本,我目前的任务之一要求我使用它。它来自更通用的链接List Type类。我已经打了几个小时这个问题了,不知道为什么它不会联系。

这是主要的:

#include <iostream>
#include <cmath>
#include "unorderedlinkedlist.h"

using namespace std;


int main()
{
unorderedLinkedList<int> newList;
for (int i = 1; i<=10;i++)
    newList.insertLast(pow(2,i));

newList.print();
return 0;
}

这是派生类的标题:

#ifndef UNORDEREDLINKEDLIST_H
#define UNORDEREDLINKEDLIST_H
#include <iostream>
#include "linkedlisttype.h"


using namespace std;

template <class Type>
class unorderedLinkedList : public linkedListType<Type>
{
    public:
        /// Check to see if item already exists in the list
        bool search(const Type& searchItem) const;

        /// insert newItem at the beginning of the list
        void insertFirst(const Type& newItem);

        ///function to insert newItem at the end of the list
        void insertLast(const Type& newItem);

        /// delese deleteItem from the list
        void deleteNode(const Type& deleteItem);


    protected:

    private:
};


template <class Type>
bool unorderedLinkedList<Type>::search(const Type& searchItem) const
{
    nodeType<Type> *current;
    bool found = false;
    current = linkedListType<Type>::first;

    while (current != NULL && !found)
        if (current->info == searchItem)
            found = true;
        else current = current->link;

    return found;
}

template <class Type>
void unorderedLinkedList<Type>::insertFirst(const Type& newItem)
{
    nodeType<Type> *newNode;
    newNode = new nodeType<Type>;
    newNode->info = newItem;
    newNode->link = linkedListType<Type>::first;
   linkedListType<Type>::first = newNode;

    linkedListType<Type>::count++;

    if (linkedListType<Type>::last == NULL)
        linkedListType<Type>::last = newNode;
}

template <class Type>
void unorderedLinkedList<Type>::insertLast(const Type& newItem)
{
    nodeType<Type> *newNode;
    newNode = new nodeType<Type>;
    newNode->info = newItem;
    newNode->link = NULL;

    if (linkedListType<Type>::first == NULL)
    {
        linkedListType<Type>::first = newNode;
        linkedListType<Type>::last = newNode;
        linkedListType<Type>::count++;
    }
    else
    {
        linkedListType<Type>::last->link = newNode;
        linkedListType<Type>::last = newNode;
        linkedListType<Type>::count++;
    }
}

template <class Type>
void unorderedLinkedList<Type>::deleteNode(const Type& deleteItem)
{
   nodeType<Type> *current;
    nodeType<Type> *trailCurrent;
    bool found;

    if (linkedListType<Type>::first == NULL)
        cout << "You cannot delete soemthing from an empty list." << endl;

    else
    {
       if (linkedListType<Type>::first ->info == deleteItem)
        {
            current = linkedListType<Type>::first;
            linkedListType<Type>::first = linkedListType<Type>::first->link;
            linkedListType<Type>::count--;

            if (linkedListType<Type>::first == NULL)
                linkedListType<Type>::last = NULL;

            delete current;
        }
        else
        {
            found = false;
            trailCurrent = linkedListType<Type>::first;
            current = linkedListType<Type>::first->link;

            while (current != NULL && !found)
            {
                if(current->info != deleteItem)
                {
                    trailCurrent = current;
                    current = current->link;
                }
                else
                    found = true;
            }


            if(found)
            {
                trailCurrent->link = current->link;
                linkedListType<Type>::count--;

                if (linkedListType<Type>::last == current)
                linkedListType<Type>::last = trailCurrent;

                delete current;
            }
            else
                cout << "the item to be deleted is not in the list." << endl;
        }
    }
}
#endif // UNORDEREDLINKEDLIST_H

链接器错误:

  

/tmp/cc9ozgPv.o:       (.rodata._ZTV14linkedListTypeIiE [_ZTV14linkedListTypeIiE] + 0x20):对linkedListType<int>::search(int const&) const' /tmp/cc9ozgPv.o:(.rodata._ZTV14linkedListTypeIiE[_ZTV14linkedListTypeIiE]+0x28): undefined reference to的未定义引用linkedListType :: insertFirst(int const&amp;)'    /tmp/cc9ozgPv.o:(.rodata._ZTV14linkedListTypeIiE[_ZTV14linkedListTypeIiE]+0x30):对linkedListType<int>::insertLast(int const&)' /tmp/cc9ozgPv.o:(.rodata._ZTV14linkedListTypeIiE[_ZTV14linkedListTypeIiE]+0x38): undefined reference to的未定义引用linkedListType :: deleteNode(int const&amp;)'       collect2:错误:ld返回1退出状态

0 个答案:

没有答案