试图通过一个b类函数访问类成员,出了什么问题?

时间:2015-12-26 02:48:29

标签: c++ class arraylist

append()函数中,我尝试通过类User访问类Arraylist成员,其中我有一个User类型的数组,任何帮助我如何访问他们?

class User 
{
    string username;
    string password;

    void login();
    void logout();
    void signup();
    void search();
};

class ArrayList
{
public:
    User* root;
    User* elems;
    int capacity;
    int elemsCnt;
    void expand();
    ArrayList();
    ~ArrayList();
    void append(string, string);
};

void ArrayList::append(string name, string pass)
{
    if (capacity == elemsCnt)
        expand();

    elems[elemsCnt++].username = name;
    elems[elemsCnt++].password = pass;
} 

1 个答案:

答案 0 :(得分:2)

默认情况下,类成员是私有的。您需要将它们公开以从外部访问它们:

class User 
{
public: // Add this!
    string username;
    string password;

    void login();
    void logout();
    void signup();
    void search();
};