c ++使用用模板构建的对象

时间:2015-04-02 22:49:41

标签: c++ templates generics

我使用类模板构建了一个链表,使其成为通用的。它都编译,但我无法测试它,因为我无法创建它的实例。

节点类

template <class T>
struct MyLinkedListNode
{
    T Data;
    MyLinkedListNode<T>* Next;
    MyLinkedListNode<T>* Previous;
    MyLinkedListNode(T data, MyLinkedListNode<T>* next, MyLinkedListNode<T>* previous);
    ~MyLinkedListNode();
};

并实施

#include "stdafx.h"
#include "MyLinkedListNode.h"

template <class T>
MyLinkedListNode<T>::MyLinkedListNode(T data, MyLinkedListNode<T>* next, MyLinkedListNode<T>* previous)
{
    Data = data;
    Next = next;
    Previous = previous;
}
template <class T>
MyLinkedListNode<T>::~MyLinkedListNode()
{
    if (Next)
    {
        Next->Previous = nullptr;
        delete Next;
    }
    if (Previous)
    {
        delete Previous;
    }
}

列表相同......

#include "MyLinkedListNode.h"
template <class T>
class MyLinkedList
{
    MyLinkedListNode<T>* Tail;
public:
    int Count;
    MyLinkedListNode<T>* Head;
    MyLinkedList();
    void Add(T value);
    int Insert(T value, int index);
    MyLinkedListNode<T>* GetNode(int index);
    int Get(int index);
    int RemoveHead();
    int RemoveAt(int index);
    int RemoveTail();
    string ToString();
    void Clear();
    int Search(int value);
    void PrintList();
    ~MyLinkedList();
};

并实施

#include "stdafx.h"
#include "MyLinkedList.h"

template <class T>
MyLinkedList<T>::MyLinkedList()
{
    Count = 0;
}

template <class T>
void MyLinkedList<T>::Add(T value)
{
    MyLinkedListNode<T>* newNode = new MyLinkedListNode<T>(value, nullptr, nullptr);
    if (Head == nullptr)
    {
        Head = newNode;
        Tail = newNode;
    }
    else
    {
        Tail->Next = newNode;
        newNode->Previous = Tail;
        Tail = newNode;
    }
    Count++;
}

template <class T>
int MyLinkedList<T>::Insert(T value, int index)
{ 
    if (index == 0)
    {
        MyLinkedListNode<T>* newNode = new MyLinkedListNode<T>(value, Head, nullptr);
        Head->Previous = newNode;
        Head = newNode;
    }
    else if (index == Count)
    {
        Add(value);
    }
    else
    {
        MyLinkedListNode<T>* next = GetNode(index);
        MyLinkedListNode<T>* prev = next->Previous;
        prev->Next = new MyLinkedListNode<T>(value, next, prev);
    }
    return 0;
}

template <class T>
MyLinkedListNode<T>* MyLinkedList<T>::GetNode(int index)
{ 
    MyLinkedListNode<T>* current = Head;
    if (index >= 0 && index < Count && current != nullptr)
    {
        for (int i = 0; i < index; i++)
        {
            current = current->Next;
        }
        return current;
    }
    throw - 1;
}

template <class T>
int MyLinkedList<T>::Get(int index)
{ 
    MyLinkedListNode<T>* current = Head;
    if (index >= 0 && index < Count && current != nullptr)
    {
        for (int i = 0; i < index; i++)
        {
            current = current->Next;
        }
        return current->Data;
    }
    throw -1;
}

template <class T>
int MyLinkedList<T>::RemoveHead()
{ 
    if (Head == nullptr)
    {
        return -1;
    }
    else
    {
        if (Count == 1)
        {
            delete Head;
            Head = nullptr;
            Tail = nullptr;
        }
        else
        {
            MyLinkedListNode<T>* temp = Head;
            Head = temp->Next;
            Head->Previous = nullptr;
            temp->Next = nullptr;
            delete temp;
        }
        Count--;
        return 0; 
    }
}

template <class T>
int MyLinkedList<T>::RemoveAt(int index)
{ 
    if (index == 0)
    {
        RemoveHead();
        return 0;
    }
    else if (index == Count - 1)
    {
        RemoveTail();
        return 0;
    }
    MyLinkedListNode<T>* node = GetNode(index);
    MyLinkedListNode<T>* prev = node->Previous;
    MyLinkedListNode<T>* next = node->Next;
    prev->Next = next;
    next->Previous = prev;
    node->Next = nullptr;
    node->Previous = nullptr;
    delete node;
    Count--;
    return 0;
}

template <class T>
int MyLinkedList<T>::RemoveTail()
{ 
    if (Tail == nullptr)
    {
        return -1;
    }
    else
    {
        if (Count == 1)
        {
            delete Head;
            Head = nullptr;
            Tail = nullptr;
        }
        else
        {
            MyLinkedListNode<T>* temp = Tail;
            Tail = temp->Previous;
            Tail->Next = nullptr;
            temp->Previous = nullptr;
            delete temp;
        }
        Count--;
        return 0;
    }
}

template <class T>
string MyLinkedList<T>::ToString()
{ 
    string result = "{";
    MyLinkedListNode<T>* current = Head;
    while (current != nullptr)
    {
        result = result + " " + to_string(current->Data) + ",";
        current = current->Next;
    }
    result = result.substr(0, result.length() - 1) + " }";
    return result;
}

template <class T>
void MyLinkedList<T>::Clear()
{
    Count = 0;
    delete Head;
    Head = nullptr;
    Tail = nullptr;
}

template <class T>
int MyLinkedList<T>::Search(int value)
{ 
    MyLinkedListNode<T>* current = Head;
    int index = 0;
    while (current != nullptr)
    {
        if (current->Data == value)
        {
            return index;
        }
        current = current->Next;
        index++;
    }
    return -1; 
}

template <class T>
void MyLinkedList<T>::PrintList()
{
    if (Count > 0)
    {
        int i = 0;
        MyLinkedListNode<T>* current = Head;
        while (current != nullptr)
        {
            cout << "#" << i << "  " << current->Data << endl;
            i++;
            current = current->Next;
        }
        cout << endl;
    }
    return 0;
}

template <class T>
MyLinkedList<T>::~MyLinkedList()
{
    delete Head;
}

那么如果我的主要看起来像

#include "stdafx.h"
#include "MyLinkedList.h"

int main()
{
    MyLinkedList<int>* list = new MyLinkedList<int>();
    return 0;
}

我收到错误Driver.obj:错误LNK2019:函数_main中引用的未解析的外部符号“public:__thiscall MyLinkedList :: MyLinkedList(void)”(?? 0?$ MyLinkedList @ H @@ QAE @ XZ)

我不明白。我猜我的模板做错了。如果我删除main中的代码,它构建得很好,所以我真的很茫然。

0 个答案:

没有答案