-2

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;
    };                                          
};
Ruaa Brkat
  • 151
  • 1
  • 2
  • 14
  • There are three syntax errors in your program: `next=NULL:` should be `next=NULL;` -- `Error_code outcome SUCCESS;` should be `Error_code outcome = SUCCESS;` -- `n= new Node;` should be `n= new Node;` – dyp Oct 26 '13 at 13:40
  • `next` is private in `Node` class. – cppcoder Oct 26 '13 at 13:43
  • I corrected the syntax errors @DyP noticed, I changed _next_ to public, but now I get this error : Error 5 error LNK2019: unresolved external symbol "public: __thiscall queue::~queue(void)" (??1?$queue@H@@QAE@XZ) referenced in function _main – Ruaa Brkat Oct 26 '13 at 13:49

1 Answers1

1

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

4pie0
  • 29,204
  • 9
  • 82
  • 118
  • "change it to be public or redesign the program" or befriend `queue` – dyp Oct 26 '13 at 13:45
  • I changed it to public, but now I get this error : Error 5 error LNK2019: unresolved external symbol "public: __thiscall queue::~queue(void)" (??1?$queue@H@@QAE@XZ) referenced in function _main – Ruaa Brkat Oct 26 '13 at 13:46
  • @user2908749 You have declared, but not defined the ctor and dtor of `queue` in your example. – dyp Oct 26 '13 at 13:48
  • @DyP I did that in the .cpp file – Ruaa Brkat Oct 26 '13 at 13:51
  • @DyP I moved the definition of ctor and dtor to the header file, it now works. – Ruaa Brkat Oct 26 '13 at 13:54
  • @user2908749 See http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file – dyp Oct 26 '13 at 13:56