0

I have the following classes:

template <class S, class T>
class Pair {

protected:
    S data1;
    T data2;

public:
  ...
}

template <class S, class T>
class Node: public Pair<const KeyType, DataType> {

private:
    Node<const S, T>* next;

public:

    //C'tor
    Node(const KeyType& sKey, const DataType& sData) :
    data1(sKey), data2(sData), next(NULL) {
    }

    //Copy C'tor
    Node(const Node<KeyType, DataType>& sNode):
    data1(sNode.getData1()), data2(sNode.getData2()), next(NULL) {
    }

    ...
};

But for some reason I can't manage to work with data1 and data2 whom Node inherited from Pair. I get the following errors:

./map.h:24:5: error: use of undeclared identifier 'data1'
                                data1 = sKey;
                                ^
./map.h:25:5: error: use of undeclared identifier 'data2'
                                data2 = sData; 
                                ^
./map.h:30:4: error: member initializer 'data1' does not name a non-static data member or base class
                        data1(sNode.getData1()), data2(sNode.getData2()), next(NULL) {
                        ^~~~~~~~~~~~~~~~~~~~~~~
./map.h:30:29: error: member initializer 'data2' does not name a non-static data member or base class
                        data1(sNode.getData1()), data2(sNode.getData2()), next(NULL) {
                                                 ^~~~~~~~~~~~~~~~~~~~~~~

What I'm doing wrong?

*This is part of my HW assignment so I can't use pair from the STL.

triple fault
  • 13,410
  • 8
  • 32
  • 45

1 Answers1

1

While you can probably poke through to the data members by prefixing them with the class name as I suggested in the comments; a better idea would be to create a constructor for Pair:

template <class S, class T>
class Pair {

protected:
    S data1;
    T data2;

public:
  Pair(const S & d1, const T & d2) : data1( d1), data2( d2) { }
  ...
} ;

Then you can initialize Pair by doing:

Node(const KeyType& sKey, const DataType& sData) :
  Pair(sKey, sData), next(NULL)
  ...
woolstar
  • 5,063
  • 20
  • 31