在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;
}
答案 0 :(得分:2)
默认情况下,类成员是私有的。您需要将它们公开以从外部访问它们:
class User
{
public: // Add this!
string username;
string password;
void login();
void logout();
void signup();
void search();
};