简体   繁体   English

Class inheritance 但在 C++ 中遇到重新定义错误

[英]Class inheritance but encounter redefinition error in C++

I'm learning C++ Data Structure, about implementing Stack using Array and Linked List.我正在学习 C++ 数据结构,关于使用数组和链表实现堆栈。 The array is default in C++ but I've constructed a Linked List class.该数组在 C++ 中是默认的,但我构建了一个链接列表 class。 The code below is my Linked List header file下面的代码是我的链接列表 header 文件

#pragma once
#include "Node.h"
#include "Stack.h"
class LinkedList
{
public:
    LinkedList() {head = NULL;}
    Node* AddNode(int index, float x, bool doubly);
    int FindNodeIndex(float x);
    Node* FindNodeAddress(float x);
    void DeleteNode(float x);
    void DisplayList();
    
private:
    Node* head;
    friend class Stack;
};

which also used the Node header file, but it is not relevant here.它也使用了节点 header 文件,但这里不相关。 Then I would like to implement stack using both array and linked list.然后我想同时使用数组和链表来实现堆栈。 Below is my Stack header file:下面是我的堆栈 header 文件:

#pragma once

class Stack
{
public:
    Stack(int size = 10);
    bool IsEmpty() { return top == -1;}
    bool IsFull() { return top == maxTop;}
    double Top();
    void Push(const double x);
    double Pop();
    void DisplayStack();
    void Clear();
private:
    int maxTop;
    int top;
    double* values;
};

class Stack: public LinkedList
{
public:
    Stack(){}
    double Top()
    {
        if (!head)
        {
            std::cout << "The stack is empty." << std::endl;
            return -1;
        }
        else
            return head->data;
    }
    void Push(const double x) {AddNode(0,x);}
    void DisplayStack() { DisplayList();}
}

You can see in the Top() method, which is used to find the top element of the linked list, used the head variable (or explicitly, pointer of Node), where in the implementation of array, I have the same Top() method but it only use indexed array.在Top()方法中可以看到,该方法用于查找链表的顶部元素,使用了head变量(或明确地,Node的指针),其中在数组的实现中,我有相同的Top()方法,但它只使用索引数组。 The compiler give me this error message编译器给我这个错误信息

Stack.h:20:7: error: redefinition of 'class Stack'
   20 | class Stack: public LinkedList

I knew I've probably made some mistake here, but I think it is necessary to write two Stack classes because they have different purpose, for example the Top() method cannot be used for both.我知道我可能在这里犯了一些错误,但我认为有必要编写两个 Stack 类,因为它们有不同的用途,例如 Top() 方法不能同时用于两者。 Is it possible to combine both as one Stack class, then notify the compiler whether to use array or linked list?是否可以将两者合并为一个堆栈 class,然后通知编译器是使用数组还是链表?

Hi as @Some_programmer_dude said嗨,正如@Some_programmer_dude 所说

Classes can't be "overloaded", they must have different names if they are defined in the same namespace.类不能“重载”,如果它们在同一个命名空间中定义,它们必须具有不同的名称。

I would use polymorphism for this situation.对于这种情况,我会使用多态性。 So we define a base class named Stack, then will create two other classes named LinkedListStack and ArrayStack which both derive form Stack.因此,我们定义了一个名为 Stack 的基础 class,然后将创建另外两个名为 LinkedListStack 和 ArrayStack 的类,它们都派生自 Stack。

// abstract class Stack
class Stack
{
public:
    Stack(int size = 10);
    virtual bool IsEmpty() = 0; // pure virtual function
    virtual bool IsFull();
    virtual double Top();
    virtual void Push(const double x);
    virtual double Pop();
    virtual void DisplayStack();
    virtual void Clear();
protected:
    int maxTop;
    int top;
    double* values;
};

As you can see, Stack has a member method virtual bool IsEmpty() = 0;可以看到,Stack 有一个成员方法virtual bool IsEmpty() = 0; , this is called a pure virtual function which won't have any definition, which will cause our class to be an abstract base class. ,这被称为纯虚拟 function 没有任何定义,这将导致我们的 class 成为抽象基础 class。

Abstract base classes are classes that can only be used as base classes and can't be instantiated.抽象基类是只能作为基类使用,不能实例化的类。 Since we don't want to instantiate a Stack class, we want to instantiate LinkedListStack or ArrayStack.由于我们不想实例化堆栈 class,我们想要实例化 LinkedListStack 或 ArrayStack。

Then for each derived class(LinkedListStack and ArrayStack), we'll define their base class virtual functions.然后对于每个派生类(LinkedListStack 和 ArrayStack),我们将定义它们的基本 class 虚函数。

class LinkedListStack: public Stack
{
public:
    LinkedListStack();
    bool IsEmpty() { return !head; }
    double Top()
    {
        if (IsEmpty())
        {
            std::cout << "The stack is empty." << std::endl;
            return -1;
        }
        else
            return head->data;
    }
    void Push(const double x) { AddNode(0,x); }
    void DisplayStack() { DisplayList(); }
    /* define remaining base member methods. */
}
class ArrayStack : public Stack
{
public:
    ArrayStack(int size = 10);
    bool IsEmpty() { return top == -1; }
    bool IsFull() { return top == maxTop; }
    /* define remaining base member methods. */
};

Check this amazing C++ tutorial , it teaches virtual functions and polymorphism.查看这个惊人的C++ 教程,它教授虚函数和多态性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM