user3713174
user3713174

Reputation: 13

trouble with inheritance: derived class not inheriting base class

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

And the .h for my derived class

#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

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?

EDIT: 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;
}

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  

Upvotes: 1

Views: 2884

Answers (2)

Pinna_be
Pinna_be

Reputation: 4617

Is it possible that you're trying to to initialize the members as follows?

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

because that is not allowed, you should instead call the parent constructor,

SortedSet(): Intlist(){}

the same with copy constructors:

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

This holds for each type of constructor. A second option would be to initialize those members in the body of the constructor.

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

BTW problem previously solved here: C++: Why does my DerivedClass's constructor not have access to the BaseClass's protected field?

The reason you can't access head from IntList in SortedSet(const IntList&) is described here: subtle C++ inheritance error with protected fields

Upvotes: 0

Mark Ransom
Mark Ransom

Reputation: 308111

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) // ...

Upvotes: 2

Related Questions