The code is intended to sort the initiative
values of characterbase
objects. Right now, it only sorts two characterbase
objects.
The code is overly complicated because it is intended to be able to eventually handle sorting 4 to 5 character objects. Which is why I'm using a for
for just two objects. Originally, it handled references, but I tried to switch the vector
to just hold pointers using ptrCharacterTurnOrderArray
. But the compiler is giving some errors with iterator->setCharacterTurnOrder
and other iterator attempts to access things.
Specifically, it says of the iterator->
attempts that:
Expression must have pointer to class type but it has type CharacterBase *
I am still learning pointers, so I'm sorry if this seems stupid.
player_ptr = &PC;
enemy_ptr = &Enemy;
ptrCharacterTurnOrderArray.push_back(player_ptr);
ptrCharacterTurnOrderArray.push_back(enemy_ptr);
for (auto iterator = ptrCharacterTurnOrderArray.begin(); iterator != ptrCharacterTurnOrderArray.end(); iterator ) {
engineFeedback = engineFeedback "\n Name: " iterator->getCharacterName() " Initiative: " std::to_string(iterator->getCharacterInitiative()) " Player Character Flag: " std::to_string(iterator->getIsPlayerCharacter());
iterator->setCharacterTurnOrder(iterator - ptrCharacterTurnOrderArray.begin() 1);
engineFeedback = engineFeedback "\nTurn Order: " std::to_string(iterator->getCharacterTurnOrder());
}
CodePudding user response:
Applying either the ->
or *
operator directly on an iterator returns a reference to the thing being referred to.
So, using iterator->...
works fine to access class members when a vector holds object instances.
But, when a vector holds pointers instead, you have to first dereference an iterator to access the actual pointer before then using the ->
operator to access any class members via that pointer, eg (*iterator)->...