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

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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?