Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to properly use protected in singly linked list
    primarykey
    data
    text
    <p>I've successfully programmed a singly linked list by the following program: The header file is:</p> <pre><code>#ifndef SLL_H_ #define SLL_H_ #include &lt;iostream&gt; class node { protected: public: int key; node *next; node(); ~node(); }; class SLL : public node{ private: node *Head = NULL; int SLL_SIZE = 0; public: //Constructor SLL(); //SLL(int n); //Destructor ~SLL(); //Modifiers void Push_Front(int a); void Push_Back(SLL A,int b); void Traverse(); //Access function int SLL_size(); int Get(node* p); //Iterator node* Begin(); node* End(); //void Search(int a); }; #endif </code></pre> <p>SLL.cpp</p> <pre><code>#include "SLL.h" #include &lt;iostream&gt; using namespace std; node::node(){ cout &lt;&lt; "Empty constructor of node is being called;" &lt;&lt; endl; } node::~node(){ cout &lt;&lt; "Empty destructor of node is being called;" &lt;&lt; endl; } SLL::SLL():node(){ cout &lt;&lt; "Empty constructor of SLL is being called;" &lt;&lt; endl; } SLL::~SLL(){ cout &lt;&lt; "Empty destructor of SLL is being called." &lt;&lt; endl; } //Insert element at the front of the list void SLL::Push_Front(int k){ node *temp = new node [1]; temp-&gt;key = k; temp-&gt;next = Head; Head = temp; SLL_SIZE = SLL_SIZE + 1; } //Insert element at the end of the list void SLL::Push_Back(SLL A, int m){ node *temp1 = A.End(); node *temp2 = new node [1]; temp2-&gt;key = m; temp1-&gt;next = temp2; temp2-&gt;next = NULL; SLL_SIZE = SLL_SIZE + 1; } //Insert element at a given position //Return the number of elements in the linked list int SLL::SLL_size(){ return SLL_SIZE; } //Traverse the list (print the list) void SLL::Traverse(){ node *temp; temp = Head; while(temp!=NULL){ cout &lt;&lt; temp-&gt;key &lt;&lt; " "; temp = temp-&gt;next; } cout &lt;&lt; endl; } //Get key given pionter int SLL::Get(node* pt){ if(pt!=NULL){ node* temp = pt; return temp-&gt;key; } else { cout &lt;&lt; "Null pointer points to nowhere!" &lt;&lt; endl; return 0; } } //Return the pointer at the beginning of the list node* SLL::Begin(){ return Head; } //Return the pointer at the end of the list node* SLL::End(){ node* temp = Head; while(temp-&gt;next!=NULL){ temp = temp-&gt;next; } return temp; } </code></pre> <p>main.cpp</p> <pre><code>#include &lt;iostream&gt; #include "SLL.h" using namespace std; int main() { SLL A; A.Push_Front(1); A.Push_Front(2); A.Push_Front(5); A.Push_Front(6); A.Push_Back(A,3); A.Traverse(); cout &lt;&lt; A.SLL_size() &lt;&lt; endl; cout &lt;&lt; A.Get(A.Begin()) &lt;&lt; endl; cout &lt;&lt; A.Get(A.End()) &lt;&lt; endl; return 0; } </code></pre> <p>One error is, for example :</p> <pre><code>SLL.h||In member function 'void SLL::Push_Front(int)':| SLL.h|7|error: 'int node::key' is protected| SLL.cpp|25|error: within this context| SLL.h|8|error: 'node* node::next' is protected| SLL.cpp|26|error: within this context| SLL.h||In member function 'void SLL::Push_Back(SLL, int)':| SLL.h|7|error: 'int node::key' is protected| SLL.cpp|35|error: within this context| SLL.h|8|error: 'node* node::next' is protected| LL.cpp|36|error: within this context| SLL.h|8|error: 'node* node::next' is protected| SLL.cpp|37|error: within this context| </code></pre> <p>Similar error for the other member functions that employed key and next.</p> <p>This program works very well now. However, after I moved the 2 lines in <code>node</code> class, <code>int key; node *next;</code> under <code>protected</code>, then it gives me error such as "node::key is protected".</p> <p>First, please don't blame me for doing something stupid :P . I know if I <code>struct</code> for the node then life will be much easier. I am trying to practice inheritance and to understand protected. That's why.</p> <p>By definition, protected members can be accessed by derived class, right? I don't know where I am doing it wrong.</p> <p>Hope you can help me out. Thanks! </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload