本文介绍了麻烦的继承:派生类不继承基类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有继承的麻烦,我得到了背后的想法,但由于某种原因,我的代码只是不会合作。这是我的基本头文件:

Having trouble with inheritance, I get the idea behind it but for some reason my code just won't cooperate. Here is my base header file:

#ifndef INTNODE_H
#define INTNODE_H

struct IntNode

{
int data;
IntNode *next;
IntNode( int data ) : data(data), next(0) {}

};


class IntList
{
protected:
    IntNode *head;
    IntNode *tail;
public:
    IntList();
    IntList( const IntList & );
    ~IntList();
    void display()const;
    void push_front(int);
    void push_back(int);
    void pop_front();
    void select_sort();
    void insert_sorted(int);
    void remove_duplicates();
    const IntList & operator = (const IntList &);
    void clear();
};
#endif

而我的派生类的.h

#ifndef SORTEDSET_H
#define SORTEDSET_H
#include "IntList.h"


class SortedSet : public IntList
{
public:
    SortedSet();
    SortedSet(const SortedSet &);
    SortedSet(const IntList &);
    ~SortedSet();
    bool in (int);
    const SortedSet operator |(const SortedSet);
    const SortedSet operator &(const SortedSet);
    void add (int );
    void push_front(int );
    void push_back(int);
    void insert_sorted(int);
    SortedSet operator  |=(SortedSet);
    SortedSet operator &=(SortedSet);
  };


#endif

但是当我尝试编译时,它告诉我我的SortedSet类没有head或tail数据成员,这让我认为SortedSet从来没有实际继承bas类。不确定我做错了什么?

but when I try to compile, it tells me my SortedSet class doesn't have "head" or "tail" data members which makes me think that SortedSet is never actually inheriting the bas class. Not sure what I'm doing wrong?

编辑:这里是派生类的.cpp,基类的.cpp是相当冗长,但我可以post

here is the .cpp for the derived class, the .cpp for the base class is rather lengthy but i can post that too

#include <cstdlib>
#include <iostream>
#include "SortedSet.h"

using namespace std;

SortedSet::SortedSet():head(0),tail(0){}

SortedSet::SortedSet(const SortedSet &s):head(0),tail(0)
{
    for (IntNode *i=s.head;i!=0;i=i->next)
    {
          push_back(i->data);
    }
}

SortedSet::SortedSet(const IntList &l)
{
    for (IntNode *i=l.head;i!=0;i=i->next)
    {
          push_back(i->data);
    }
    remove_duplicates();
    select_sort();
}

SortedSet::~SortedSet(){}

SortedSet::in(int d)
{
    for(IntNode *i=head;i!=0;i=i->next)
    {
        if(i->data==d)
            return true;
    }
    return false;
}

和herE是我得到的错误

and herE are the errors I'm getting

IntList.cpp: In member function 'const IntList& IntList::operator=(const IntList&)':
IntList.cpp:226: warning: control reaches end of non-void function
SortedSet.cpp: In constructor 'SortedSet::SortedSet()':
SortedSet.cpp:27: error: class 'SortedSet' does not have any field named 'head'
SortedSet.cpp:27: error: class 'SortedSet' does not have any field named 'tail'
SortedSet.cpp: In copy constructor 'SortedSet::SortedSet(const SortedSet&)':
SortedSet.cpp:29: error: class 'SortedSet' does not have any field named 'head'
SortedSet.cpp:29: error: class 'SortedSet' does not have any field named 'tail'
IntList.h: In constructor 'SortedSet::SortedSet(const IntList&)':
IntList.h:35: error: 'IntNode* IntList::head' is protected
SortedSet.cpp:39: error: within this context
SortedSet.cpp: At global scope:
SortedSet.cpp:49: error: ISO C++ forbids declaration of 'in' with no type


推荐答案

构造函数的初始化列表无法初始化父类的任何成员。你应该调用父构造函数来做正确的初始化。

The initialization list of a constructor isn't able to initialize any members of a parent class. Instead, you should call the parent constructor that will do the proper initialization.

    SortedSet::SortedSet(const SortedSet &s) : IntList(s) // ...

这篇关于麻烦的继承:派生类不继承基类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 20:02