YGJ_SJTU 2018-05-08 03:49 采纳率: 100%
浏览 5825
已采纳

C++ 编译错误: Error:undefined reference to ` '

我编写了一个从list母类继承的链表linkList子类并把声明写在linkList.h文件中,实现写在linkList.cpp中,
但是我发现只用#include "linkList.h"的话会报错如下图片说明

但是如果加上#include "linkList.cpp" 则不会报错。

希望大神告知错误所在,以及教我一下如何正确的书写头文件和源文件!

以下是代码:

linkList.h:

#ifndef LINKLIST_H_INCLUDED
#define LINKLIST_H_INCLUDED

class OutOfBound {};
class IllegalSize {};

template <class T>
class list
{
public:
    virtual int length() const = 0;
    virtual void clear() = 0;
    virtual void insert(int i,const T& x) = 0;
    virtual void remove(int i) = 0;
    virtual T visit(int i) const = 0;
    virtual int search(const T& x) const = 0;
    virtual void traverse() const = 0;
    virtual ~list() {};
};


template <class T>
class linkList:public list<T>
{
private:
    struct node
    {
            T data;
            node *prev, *next;

            node(const T &x, node* p = NULL, node* n = NULL)
            {
                    data = x;
                    prev = p;
                    next = n;
            }
            node():next(NULL), prev(NULL) {}
            ~node() {}
    };

    node *head, *tail;
    int currentLength;

    node* move(int i) const;

public:
    linkList();
    ~linkList()
    {
            clear();
            delete head;
            delete tail;
    }

    int length() const
    {
            return currentLength;
    }
    void clear();
    void insert(int i,const T& x);
    void remove(int i);
    T visit(int i) const;
    int search(const T& x) const;
    void traverse() const;
};
#endif // LINKLIST_H_INCLUDED


linkList.cpp:
//file:
#include <iostream>
#include <cstdio>
#include "linkList.h"
using namespace std;


template <class T>
typename linkList<T>::node* linkList<T>::move(int i) const
{
    node* p = head -> next;
    if(i < 0 || i > currentLength) throw OutOfBound();
    while(i > 0)
    {
            p = p -> next;
            i--;
    }
    return p;
}

template <class T>
linkList<T>::linkList()
{
    head = new node;
    tail = new node;
    head -> next = tail;
    tail -> prev = head;
    currentLength = 0;
}

template <class T>
void linkList<T>::clear()
{
    node *p, *q;
    p = head -> next;
    while(p != tail)
    {
            q = p -> next;
            delete p;
            p = q;
    }
    head -> next = tail;
    tail -> prev = head;
    currentLength = 0;
}

template <class T>
void linkList<T>::insert(int i, const T& x)
{
    node *pos = move(i);
    node *tmp = new node(x, pos -> prev, pos);
    pos -> prev -> next = tmp;
    pos -> prev = tmp;
    ++currentLength;
}

template <class T>
void linkList<T>::remove(int i)
{
    node *pos = move(i);
    pos -> prev -> next = pos -> next;
    pos -> next -> prev = pos -> prev;
    delete pos;
    --currentLength;
}

template <class T>
int linkList<T>::search(const T& x) const
{
    int i = 0;
    node* p = head -> next;
    while(p != tail && p -> data != x)
    {
            p = p -> next;
            i++;
    }

    if( p == tail)
            return -1;
    else
            return i;
}

template <class T>
T linkList<T>::visit(int i) const
{
    node* p = move(i);
    return p -> data;
}

template <class T>
void linkList<T>::traverse() const
{
    node *p = head -> next;
    while(p != tail)
    {
            cout << p -> data << " ";
            p = p -> next;
    }
    cout << endl;
}

main.cpp:

#include <iostream>
#include "linkList.h"

using namespace std;

int main()
{
    linkList<int> l1;
    int i;
    char ch;
    for(i = 0; i < 100; i++)
    {
            l1.insert(i, i);

    }
    l1.traverse();
    ch = cin.get();
    for(i = 50; i > 0; i--)
    {
            l1.remove(i);

    }
    l1.traverse();
    ch = cin.get();

    cout << l1.length() << endl;
    ch = cin.get();

    for(i = 0; i < l1.length() ; ++i)
    {
            cout << l1.visit(i) << endl;
    }

    l1.traverse();
    ch = cin.get();

    for(i = 60; i < 80 ; ++i)
    {
            cout << l1.search(i) << endl;
    }

    l1.traverse();
    ch = cin.get();

    return 0;
}
  • 写回答

5条回答 默认 最新

  • 不想落后的兔子 2018-05-08 05:31
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 我这模型写的不对吗?为什么lingo解出来的下面影子价格这一溜少一个变量
  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波