Fili 0 Light Poster

I want to have a class LIST -- a dinamic list of rational numbers (defined previously in my RATIONAL class). I want another class STACK to inherit LIST's root and end pointers for my list.(STACK also is dinamical) Please help because it says that STACK cannot access the root and end pointers.

PS:ROOT and END are of the type NOD* where
struct NOD
{
rational data;
NOD* next;
};
:evil: :cry: :?:

Dani AI

Generated

— The common cause is C++ access control: members declared private in the base class are not accessible to a derived class. Two straightforward fixes are (a) make the node-pointer members protected (so STACK can use them), or (b) keep them private but expose the operations you need through protected/public member functions. Also use public inheritance (class Stack : public List) unless you intentionally want to hide the base interface. See C++ access control and inheritance for details: C++ access control .

Minimal pattern (showing protected members + public inheritance):

struct Node { /* data */ Node* next; };

class List {
protected:
    Node* head;
    Node* tail;
public:
    List() : head(nullptr), tail(nullptr) {}
    virtual ~List() {}
    // public list operations...
};

class Stack : public List {
public:
    void push(const T& v) {
        Node* n = new Node{v, head};
        head = n;
        if (!tail) tail = n;
    }
    T pop();
};

Design note: prefer composition in many cases — make Stack contain a List and forward push/pop to the list's public methods. That keeps the list internals encapsulated and prevents exposing low-level pointers to users:

class Stack {
    List storage;
public:
    void push(const T& v) { storage.insert_front(v); }
    T pop() { return storage.remove_front(); }
};

Extra tips: if you rely on inheritance and objects are deleted polymorphically, make the base destructor virtual. Avoid friend unless necessary. If pointer management is giving trouble, consider std::list/std::vector or smart pointers to reduce errors.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.