我试图从主类中的clsFamily类调用函数,但是,当我传递(&objUser)时,我收到一条消息,指出:
编辑:抱歉忘了提及,但我要根据用户的选择来操纵动态 vector 数组。
任何帮助将不胜感激,因为这是下周四xD的任务
int UserChoice();
int UserChoice()
{
int iChoice = 0;
int iAdults = 0;
int iChildren = 0;
cout << "How many family members do you have? \n";
cin >> iChoice;
cout << "How many of these are adults? \n";
cin >> iAdults;
cout << "How many of these are children? \n";
cin >> iChildren;
return iChoice;
return iAdults;
return iChildren;
}
class clsUser
{
private:
string m_sName;
int m_iAge;
public:
void SetName(string);
string GetName();
void SetAge(int);
int GetAge();
clsUser();
~clsUser();
clsUser(string, int);
};
//This is to group the singular users into a group using a vector
class clsFamily
{
private:
vector <clsUser> objUser;
public:
void InputFamilyDetails(vector <clsUser> *objUser);
void OutputFamilyDetails(vector <clsUser> *objUser);
};
void clsFamily::InputFamilyDetails(vector <clsUser>* objUser)
{
string sName = "";
int iAge = 0;
for (int iCount = 0; iCount < objUser->size(); iCount++)
{
cout << "Please enter the name of family member " << iCount + 1 << "\n";
cin >> sName;
cout << "Please enter the age of family member " << iCount + 1 << "\n";
cin >> iAge;
objUser->at(iCount).SetName(sName);
objUser->at(iCount).SetAge(iAge);
}
}
//This is to allow the user to input the the details of the users from the vector
void clsFamily::OutputFamilyDetails(vector <clsUser>* objUser)
{
for (int iCount = 0; iCount < objUser->size(); iCount++)
{
cout << "The name of family member " << iCount + 1 << " is " << objUser->at(iCount).GetName() << " \n";
cout << "The age of family member " << iCount + 1 << " is " << objUser->at(iCount).GetAge()<< " \n";
}
}
int main()
{
clsFamily objFamily;
*//This is the area where the problem is occuring*
objFamily.InputFamilyDetails(&objUser);
objFamily.OutputFamilyDetails(&objUser);
}
最佳答案
clsFamily
已经有一个objUser
成员。您不应该使用它,并且不要在InputFamiliyDetails
和OutputFamilyDetails
中使用参数:
class clsFamily
{
private:
vector <clsUser> objUser; // Needs to be initialized with some size, though
public:
void InputFamilyDetails();
void OutputFamilyDetails();
};
void clsFamily::InputFamilyDetails()
{
string sName = "";
int iAge = 0;
for (int iCount = 0; iCount < objUser.size(); iCount++)
{
cout << "Please enter the name of family member " << iCount + 1 << "\n";
cin >> sName;
cout << "Please enter the age of family member " << iCount + 1 << "\n";
cin >> iAge;
objUser.at(iCount).SetName(sName);
objUser.at(iCount).SetAge(iAge);
}
}
void clsFamily::OutputFamilyDetails()
{
for (int iCount = 0; iCount < objUser.size(); iCount++)
{
cout << "The name of family member " << iCount + 1 << " is " << objUser.at(iCount).GetName() << " \n";
cout << "The age of family member " << iCount + 1 << " is " << objUser.at(iCount).GetAge()<< " \n";
}
}
int main()
{
clsFamily objFamily;
objFamily.InputFamilyDetails();
objFamily.OutputFamilyDetails();
}
关于c++ - 获取编译错误: “identifier ' objUser' is undefined” in my main() function,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/60885934/