"is private within this context" is being thrown for function which should not be private (GCC 5.3.0, C++11)

18,913

By default class members are private, even you use private access specifier later, Your code is like :

template<class T>
class Queue{
    Queue(); //Implicitly private
    Queue(T item); //Implicitly private

    private: //explicit private
    Link<T>* head;
    Link<T>* end;
    int length;
};

so you need to make constructors public :

template<class T>
class Queue{
    public:
    Queue(); 
    Queue(T item);
    private:
    Link<T>* head;
    Link<T>* end;
    int length;
};

Same goes for Link<T> class template.

Share:
18,913
ceaston7
Author by

ceaston7

Updated on June 05, 2022

Comments

  • ceaston7
    ceaston7 almost 2 years

    I am attempting to create a queue, which requires the creation of another object stored in the queue. The errors are

    binary.cpp: In function ‘int main()’:
    binary.cpp:183:1: error: ‘Queue<T>::Queue(T) [with T = binary<std::basic_string<char> >*]’ is private
     Queue<T>::Queue(T item){
     ^
    binary.cpp:286:65: error: within this context
      Queue<binary<string>*>* queue = new Queue<binary<string>*>(tree);
                                                                     ^
    

    and

    binary.cpp: In instantiation of ‘Queue<T>::Queue(T) [with T = binary<std::basic_string<char> >*]’:
    binary.cpp:286:65:   required from here
    binary.cpp:132:1: error: ‘Link<T>::Link(T) [with T = binary<std::basic_string<char> >*]’ is private
     Link<T>::Link(T item){
     ^
    binary.cpp:184:7: error: within this context
      head = new Link<T>(item);
    

    The first is the instantiation of the Queue, and the second comes from the Queue's constructor, which is called in the instantiation line in the first error. The important declarations and definitions are:

    template<class T>
    class Link{
        Link(T item);
    
        private:
        T content;
        Link<T>* next;
    };
    
    template<class T>
    Link<T>::Link(T item){
        content = item;
        next = NULL;
    }
    
    template<class T>
    class Queue{
        Queue();
        Queue(T item);
    
        private:
        Link<T>* head;
        Link<T>* end;
        int length;
    };
    
    template<class T>
    Queue<T>::Queue(T item){
        head = new Link<T>(item);
        end = head;
        length = 1;
    }
    

    The Link class is declared and defined before the Queue class, and both are declared and defined before they are used in code. Thank you for your time.