繁体   English   中英

继承的麻烦:派生类不继承基类

[英]trouble with inheritance: derived class not inheriting base class

在继承方面遇到麻烦,我得到了它的支持,但是由于某种原因,我的代码无法配合。 这是我的基本头文件:

#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类。 不确定我在做什么错吗?

编辑:这是派生类的.cpp,基类的.cpp相当长,但我也可以发布

#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是我得到的错误

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  

构造函数的初始化列表无法初始化父类的任何成员。 相反,您应该调用将进行正确初始化的父构造函数。

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

您是否可能尝试按以下方式初始化成员?

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

因为不允许这样做,您应该改为调用父构造函数,

SortedSet(): Intlist(){}

复制构造函数相同:

SortedSet(const SortedSet& other): Intlist(other){}

这适用于每种类型的构造函数。 第二种选择是在构造函数的主体中初始化那些成员。

SortedSet(){
    // do stuff with haid and tail
}

BTW问题以前在这里解决了: C ++:为什么我的DerivedClass的构造函数无法访问BaseClass的protected字段?

此处描述了您无法从SortedSet(const IntList&)中的IntList访问head的原因: 带有受保护字段的细微C ++继承错误

暂无
暂无

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

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