Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
391 views
in Technique[技术] by (71.8m points)

c++ - cannot access private member declared in class: queue class template

I have to write code to implement template queue I get this error : cannot access private member declared in class at this line

    front=front->next;

this is the header file of my code where I get the error:

#include <iostream>
#pragma once
using namespace std;
typedef int Error_code;
#define SUCCESS 0
#define OVERFLOW -1
#define UNDERFLOW -2

template <class T>
class Node{
T item;
Node * next;
Node(){item=0; next=NULL;}
Node(T n){item=n; next=NULL:}
};

template <class T>
class queue
{
protected:

    Node<T>* front;                                             // pointer to front of Queue
    Node<T> * rear;                                             // pointer to rear of Queue
    int count;                                                  // current number of items in Queue



public:
    queue();                                        
    ~queue();
    bool isempty(){
    //return count == 0;
    if(front==NULL)
        return true;
    else 
        return false;
    };
    bool isfull(){return false;};
    Error_code serve(){
    Error_code outcome = SUCCESS;
    Node<T> *p;
    if(isempty()){
        cout<<"empty queue";
        outcome=UNDERFLOW;
    }
    else{
    p=front;
    front=front->next;
    delete p;
    count--;
    }
    return outcome;
    } ;
    Error_code retrieve(T &item){
    Error_code outcome SUCCESS;
    if(isempty())
    {           // front node is empty, queue is empty
        //return false;
        cout<<"empty queue";
        outcome=UNDERFLOW;
    }
    return outcome;
    };      


    Error_code append(T item){


    Node<T> * n ;
    n= new Node;                // create node
    n->item = item;                 // set node pointers
    n->next = NULL;     


    if (isempty())      
        {
            rear=front = n; 
        }
    else
    {
        rear->next = n;             // else place at rear
    rear = n;                           // have rear point to new node
    }
    count++;
    return SUCCESS;
    };                                          
};
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

front is a pointer to Node, but next is a private member in Node

template <class T>
class Node{
T item;                         // since you didn't specify access level
Node * next;                    // explicitly the access is private by default
Node(){item=0; next=NULL;}
Node(T n){item=n; next=NULL:}
};

so you cannot use it in queue class:

front=front->next; // error

change it to be public or redesign the program


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...