ITPub博客

首页 > > > 我写了个链表,不知道哪里错了,编译通不过,求助

我写了个链表,不知道哪里错了,编译通不过,求助

作者:z_paul 时间:2019-09-24 11:39:40 0 删除 编辑
#ifndef CLIST_H
#define CLIST_H

#define NULL 0

template <typename T>
struct Node{
    T data;
    Node* next;
    Node():data(T()), next(NULL){}
};

template <typename T>
class Clist
{
public:
        Clist();
        virtual ~Clist();

public:
        void InsertAt(int i, T data);
        void DeleteAt(int i);
        bool Length()
        {
                return Count;
        }
        void Print();
protected:
        Node<T>* Head;
        int Count;

};

#endif // CLIST_H

//===============Clist.cpp=============//


#include "Clist.h"
#include <cassert>
#include <iostream>
using namespace std;

template <typename T>
Clist<T>::Clist():Head(NULL), Count(0)
{
}

template <typename T>
Clist<T>::~Clist()
{
    if (Head != NULL)
    {
        Node<T> *p = Head;
        Head = Head->next;
        delete p;
    }
}



template <typename T>
void Clist<T>::InsertAt(int i, T data)
{
        assert(i >= 0 && i <= Count+1);
        if (i == 0)   //表示链表刚刚创建还没有结点 , 那么我给它一个结点 .
        {
                assert(Count == 0);
                Node<T> *p = new Node<T>;
                p->data = data;
                p->next = NULL;
                Head = p;
                Count += 1;
        }
        else if (i == 1)   //我插的这个结点将会是List的第一个结点(头结点) .
        {
                Node<T> *p = new Node<T>;
                p->data = data;
                p->next = Head;
                Head = p;
                Count += 1;
        }
        else
        {
                Node<T> *p = new Node<T>;
                p->data = data;
                Node<T> *tmp = new Node<T>;
                tmp = Head;
                for (int j = 1; j < i; j ++) tmp = tmp->next;
                p->next  = tmp->next;
                tmp->next = p;
                Count += 1;
        }
}


template <typename T>
void Clist<T>::DeleteAt(int i)
{
        assert( i <= Count && i > 0);
        if (i == 1)                             //表示要删除链表的首元素
        {
                Node<T> *p;
                p = Head;
                Head = Head->next;
                delete p;
                Count -= 1;
        }
        else                                    //删除的是除首元素之外的其它元素
        {
                Node<T> *p;
                Node<T> *q = Head;
                for (int j = 1; j < i; j ++) q = q->next;
                p = q->next;
                q = q->next->next;
                delete p;
                Count -= 1;
        }
}

template <typename T>
void Clist<T>::Print()
{
        Node<T> *p = Head;
        if (p != NULL)
        {
                cout << p->data << " ";
                p = p->next;
        }
}
//==============main.cpp==============//

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

int main()
{
        Clist<int> L;
        L.InsertAt(0, 2);
        L.InsertAt(1, 1);
        L.InsertAt(3, 4);
        L.InsertAt(3, 3);
        L.Print();
        return 0;
}

求大神帮帮忙啊.  编译好像也不报错啊,但是提示错误是这样的:

g++  -o bin/Debug/list obj/Debug/src/Clist.o obj/Debug/src/main.o   
obj/Debug/src/main.o:在函数‘main’中:
/home/q/workspace/list/src/main.cpp:7:对‘Clist<int>::Clist()’未定义的引用
/home/q/workspace/list/src/main.cpp:8:对‘Clist<int>::InsertAt(int, int)’未定义的引用
/home/q/workspace/list/src/main.cpp:9:对‘Clist<int>::InsertAt(int, int)’未定义的引用
/home/q/workspace/list/src/main.cpp:10:对‘Clist<int>::InsertAt(int, int)’未定义的引用
/home/q/workspace/list/src/main.cpp:11:对‘Clist<int>::InsertAt(int, int)’未定义的引用
/home/q/workspace/list/src/main.cpp:12:对‘Clist<int>::Print()’未定义的引用
/home/q/workspace/list/src/main.cpp:13:对‘Clist<int>::~Clist()’未定义的引用
/home/q/workspace/list/src/main.cpp:13:对‘Clist<int>::~Clist()’未定义的引用
collect2: error: ld returned 1 exit status
Process terminated with status 1 (0 minute(s), 0 second(s))
0 error(s), 0 warning(s) (0 minute(s), 0 second(s))


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/75/viewspace-3164018/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-07-17

  • 博文量
    151
  • 访问量
    6021