在这种情况下是私人的“正在被抛出的功能不应该是私人的(GCC 5.3.0,C ++ 11) [英] "is private within this context" is being thrown for function which should not be private (GCC 5.3.0, C++11)

查看:216
本文介绍了在这种情况下是私人的“正在被抛出的功能不应该是私人的(GCC 5.3.0,C ++ 11)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个队列,这需要创建存储在队列中的另一个对象。错误是:

  binary.cpp:在函数'int main()'中:
binary.cpp:183: 1:error:'Queue< T> :: Queue(T)[with T = binary< std :: basic_string< char> > *]'是private
Queue< T> :: Queue(T item){
^
binary.cpp:286:65:error:在此上下文中
Queue< ;二进制< string> *> * queue = new Queue< binary< string> *>(tree);
^

  binary.cpp:在'Queue< T> :: Queue(T)[with T = binary< std :: basic_string< char> > *]':
binary.cpp:286:65:从这里需要
binary.cpp:132:1:error:'Link< T> :: Link(T)[with T =二进制LT;性病:: basic_string的<炭> > *]'是private
Link< T> :: Link(T item){
^
binary.cpp:184:7:error:在此上下文中
head =新链接< T>(item);

第一个是Queue的实例化,第二个来自Queue的构造函数,在第一个错误的实例化行中。重要的声明和定义是:

  template< class T> 
class Link {
Link(T item);

私人:
T内容;
链接< T> *下一个;
};

模板< class T>
Link< T> :: Link(T item){
content = item;
next = NULL;
}

模板< class T>
class Queue {
Queue();
队列(T item);

私人:
链接< T> * head;
Link< T> * end;
int长度;
};

模板< class T>
队列< T> :: Queue(T item){
head = new Link< T>(item);
end = head;
长度= 1;





$ b

Link类是在Queue类之前声明和定义的,并且都声明了并在代码中使用之前进行了定义。感谢您的时间。

解决方案

默认情况下,类成员是私有的,即使使用 private < code $>访问说明符,您的代码如下所示:

  template< class T> 
class Queue {
Queue(); //隐式私有
队列(T item); //隐式私有

private://显式私有
链接< T> * head;
Link< T> * end;
int长度;
};

所以你需要使构造函数成为public:

 模板< class T> 
class Queue {
public:
Queue();
队列(T item);
private:
Link< T> * head;
Link< T> * end;
int长度;
};

同样适用于连结< T> 类别模板。


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.

解决方案

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.

这篇关于在这种情况下是私人的“正在被抛出的功能不应该是私人的(GCC 5.3.0,C ++ 11)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆