-2

我必须编写代码来实现模板队列我得到这个错误:无法访问在这一行的类中声明的私有成员

    front=front->next;

这是我收到错误的代码的头文件:

#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;
    };                                          
};
4

1 回答 1

1

front是指向 的指针Node,但是next是 中的私有成员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:}
};

所以你不能在queue课堂上使用它:

front=front->next; // error

将其更改为public或重新设计程序

于 2013-10-26T13:40:59.463 回答