I have been having this issue and I can't seem to find any resources that help me.
Basically, I have to change vector<User>
to be a vector<User*>
in this function:
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;
}
I can't figure out how to call the function in my main()
function correctly.
do {
promptUserAction(players, User::IndNextUser, rooms, UserAction, badGuy1);
There is no need to wrap the players
parameter in parenthesis like (players)
whenever you refer to it. Change (players)[playernumber]
to players[playernumber]
.
In any case, nothing in the code you have shown wants a vector<User*>
, so why do you think you need this conversion? If you really need it (in code you have not shown), then the only way to "convert" a vector<T>
into a vector<T*>
is to create a new vector
and push the addresses of the 1st vector
's elements into it, eg:
vector<T> vec;
...
std::vector<T*> dest;
dest.reserve(vec.size());
for(auto &elem : vec) {
dest.push_back(&elem);
}
Alternatively:
vector<T> vec;
...
std::vector<T*> dest(vec.size());
std::transform(vec.begin(), vec.end(), dest.begin(),
[](auto &elem){ return &elem; }
);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.