【发布时间】:2021-11-30 14:00:00
【问题描述】:
我一直遇到这个问题,但我似乎找不到任何可以帮助我的资源。
基本上,我必须在此函数中将vector<User> 更改为vector<User*>:
void promptUserAction(vector<User> &players, int playernumber, const Room rooms[], string &strCh,BadGuy bad)
{
// tell user where they are and describes the room
cout << (players)[playernumber] << " you are currently in " << rooms[(players)[playernumber].getIndexofCurrentRoom()]
<< endl;
if ((players)[playernumber].getIndexofCurrentRoom()==bad.getIndexCurrentRoom())
{
cout<<"There is a bad guy ("+bad.getName()+") also in the room."<<endl;
}
//prompt user for choice
cout << "What would you like to do?" << endl << "**********************" << endl;
cin >> strCh;
}
我不知道如何正确调用 main() 函数中的函数。
do {
promptUserAction(players, User::IndNextUser, rooms, UserAction, badGuy1);
【问题讨论】:
-
我不知道这到底在问什么,但你问的只是,“我如何将
std::vector<Class>更改为并排的std::vector<Class*>,后者在哪里前者中每个对象的地址? -
不,我不这么认为。我正在尝试将矢量
更改为矢量 。不是放在一边的。我试图将其从常规向量更改为指针向量。 -
你几乎描述了你想要正是我刚才问的,所以我会再次声明:给定一个具体对象的向量,你是否希望将其转换为指向仍然具体且仍驻留在原始向量中的相同对象的指针向量?诚然,我并没有开始声称我理解为什么你想要这个,并且显示的代码没有给出你甚至需要它的理由,但尽管如此,它至少 似乎 这就是你要问的。
-
你的问题是 main() 中的玩家是一个向量
吗?只需将参数更改为 promptUserAction 以匹配并执行诸如 player[playernumber]->getIndexofCurrentRoom() 之类的操作 -
我想通了...如果我发布了整个问题,那将是大约 500 行,但我只是没有改变我最初创建原始向量的位置。从那里我所要做的就是替换所有的“。”用“->”。并更改我使用向量作为向量指针的参数的位置。