简体   繁体   English

无法访问 class (C++) 的私有成员

[英]Can not access private members of class (C++)

I developed a solution in c++ that is using linked lists.我在 c++ 中开发了一个使用链表的解决方案。 First, I used struct to create my list and the nodes (which worked perfectly), but now I am required to have Data private so i am now using classes instead.首先,我使用struct来创建我的列表和节点(效果很好),但现在我需要将 Data 设为私有,所以我现在使用classes来代替。 I can not access the private members of my two classes.我无法访问我的两个班级的私人成员。

Here is my node class这是我的节点 class

class node{                                                         //class for a node  ;
        private:
            int info;
            class node *next;
        public:
            node();
            node(int info1,node myNode){
                info1 = info;
                *next = myNode;
            }
    };
    typedef class node Node;                                        //the new class called Node

Here is my list class这是我的清单 class

    class mylist{                                                       //class for a list   ;
    private:
        Node *head;
        Node *tail;
    public:
        mylist();
        mylist(node a, node b){
        *head = a;
        *tail = b;
        }
        node getHead(){
            return *head;
        }
        node getTail(){
            return *tail;
        }
};
typedef class mylist Tlist;                                        //the new class called Tlist

Here is the function from which I am trying to access members of a class:这是 function,我试图从中访问 class 的成员:

int isEmpty(Tlist l){
    /** Return true if the head of the list is NULL and false otherwise **/
    return getHead(l.head) == NULL;   //error occurs for this line ('Node mylist::head' is private within this content)
}

You are implementing the empty check as a free function.您正在将空检查实现为免费的 function。 This function does not have access to the private members.此 function 无权访问私有成员。

Two possible solutions:两种可能的解决方案:

First is free function using public members (usage: isEmpty(l) ):首先是使用公共成员的免费 function (用法: isEmpty(l) ):

bool isEmpty(Tlist l){
    /** Return true if the head of the list is NULL and false otherwise **/
    return l.getHead() == nullptr;   //error occurs for this line ('Node mylist::head' is private within this content)
}

Second is a member function (usage: l.isEmpty() ):其次是成员 function (用法: l.isEmpty() ):

class mylist {
public:
    bool isEmpty(){
        /** Return true if the head of the list is NULL and false otherwise **/
        return head == nullptr;   //error occurs for this line ('Node mylist::head' is private within this content)
    }
}

Please note that I changed NULL (C style) to nullptr and the return value from int to bool .请注意,我将NULL (C 风格)更改为nullptr并将返回值从int更改为bool Your body of the function looked strange too, so I changed it to what seems more appropriate.你的 function 的身体看起来也很奇怪,所以我把它改成了看起来更合适的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM