【问题标题】:C++ LinkedList template with multiple inheritance?具有多重继承的 C++ 链表模板?
【发布时间】:2012-05-26 23:22:01
【问题描述】:

我正在尝试创建一个具有多态性和继承自 Stack 和 Queue 头文件的 LinkedList 类。这是我第一次尝试使用模板,但不断收到诸如“类模板已定义”或“不允许抽象类类型”之类的错误。显然我做错了,如何实现模板?

// LinkedList.h File
#include "Stack.h"
#include "Queue.h"
#include "Node.h"

using namespace std;

template <typename T>
class LinkedList : public Queue<T>, public Stack<T>
{
public:
    LinkedList();
    ~LinkedList(void);

protected:
    Node<T> *first;
    Node<T> *last;
    int numItems;
};

// LinkedList.cpp File
#include "LinkedList.h"

using namespace std;

template <typename T>
class LinkedList
{
    LinkedList()
    {
        first = NULL;
        last = NULL;
        numItems = 0;
    }

    LinkedList::~LinkedList(void)
    {
        while (first != NULL)
        {
            Node* cur = first;
            first = first->next;
            delete cur;
        }
    }

    LinkedList::clear() {}    
    LinkedList::size() {}

    // Stack Functions
    LinkedList::push(T item) {}    
    LinkedList::pop() {}    
    LinkedList::top() {}

    // Queue Functions
    LinkedList::enqueue(T item) {}    
    LinkedList::dequeue() {}    
    LinkedList::peek() {}
}

// Stack.h File (Queue is the same except push/pop/top = enqueue/dequeue/peek)
#pragma once

template <typename T> class Stack
{

public:

  virtual ~Stack() {}

  virtual int size() = 0;
  virtual void clear() = 0;
  virtual void push(T item) = 0;
  virtual T pop() = 0;
  virtual T top() = 0;

};

更新代码

// LinkedList.h File
#pragma once
#include "Stack.h"
#include "Queue.h"
#include "Node.h"

template <typename T>
class LinkedList : public Queue<T>, public Stack<T>
{
public:
    LinkedList();
    ~LinkedList(void);

    void clear();
    int size();

    void push(T item);
    T pop();
    T top();

    void enqueue(T item);
    T dequeue();
    T peek();


protected:
    Node<T> *first;
    Node<T> *last;
    int numItems;
};

// LinkedList.cpp File
#include "LinkedList.h"

class LinkedList
{
    LinkedList::LinkedList()
    {
        first = NULL;
        last = NULL;
        numItems = 0;
    }

    LinkedList::~LinkedList(void)
    {
        while (first != NULL)
        {
            Node* cur = first;
            first = first->next;
            delete cur;
        }
    }

    LinkedList::clear(){}
    LinkedList::size(){}

    // Stack Functions
    void LinkedList::push(T item){}
    T LinkedList::pop(){}
    T LinkedList::top(){}

    // Queue Functions
    void LinkedList::enqueue(T item){}
    T LinkedList::dequeue(){}
    T LinkedList::peek(){}
}


// Stack.h File (Queue is the same except push/pop/top = enqueue/dequeue/peek)
#pragma once

template <typename T> class Stack
{

public:

  virtual ~Stack() {}

  virtual int size() = 0;
  virtual void clear() = 0;
  virtual void push(T item) = 0;
  virtual T pop() = 0;
  virtual T top() = 0;

};

【问题讨论】:

    标签: c++ templates multiple-inheritance


    【解决方案1】:

    我明白了

    // LinkedList.cpp File
    

    模板定义必须在每个翻译单元上可见,您不能将它们的定义放在 cpp 文件中并期望它能够正常工作(就像常规课程一样)。所有模板函数的定义都应该出现在头文件中。

    另外,编译器错误是正确的。您正在为两个 LinkedList 类编写定义。我假设您正在寻找:

    LinkedList::LinkedList(){ ... }
    

    【讨论】:

    • 所以我应该在 LinkedList.h 文件中实现我的模板,并将我的函数作为独立的 LinkedList::FunctionName() 写在一个 cpp 文件中,而不是一个类的一部分?
    • @LF4:这通常是普通课程的情况。编写模板时,需要在头文件中定义函数。它们是在类内定义还是类外定义大多无关紧要,除非当定义在类外时,您应该声明它inline
    • 我已将更新的代码包含在我认为您试图解释的内容中。我想我的问题是在哪里/如何编写模板类的函数?这一切都在模板中?感谢您的帮助。
    • @LF4:您的更新代码仍然在讨论模板的 cpp 文件。尝试使用您的 cpp 文件,移动其定义并使用它们替换头文件中的定义。您仍然会遇到错误,但您会更接近解决方案。
    • 哇,我希望老师们能明智地选择他们的话。我有 3 个说“头文件中没有代码”,而应该是“头文件中没有代码,除了您稍后将了解的模板”。谢谢那是我一直打的砖墙。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-09
    • 1970-01-01
    • 2017-01-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多