Home > other >  Trouble converting refernced function to pointer function. (Expression must have pointer-to-class ty
Trouble converting refernced function to pointer function. (Expression must have pointer-to-class ty

Time:12-08

So, full admission, I am new to C , and I don't really get pointers entirely. I started these methods using just references, and it complied fine. But once I changed the vector to a vector that holds pointers to objects instead of just objects, things just aren't working the way they are supposed to. I've tried looking through my book for anything, but it really only briskly covers pointers. So, I'm here without much a clue of what I am mucking up. The goal is to simply have the vector hold pointers to the characterbase objects that were made. Pc and enemy. Then the for code display and set values that the pointer held in the array, well, points too. I thought it might be an easy go, but something's clearly the matter. The sort comparator I made, even when removing the for methods, crashes when I test it. Just err, pointers (pun intended) in the right direction would be helpful.

void EVoot::IRollTwoCombatantsPtrs(CharacterBase& PC, CharacterBase& Enemy) {

CharacterBase* player_ptr = nullptr;
CharacterBase* enemy_ptr = nullptr;
player_ptr = &PC;
enemy_ptr = &Enemy;

std::vector<CharacterBase*> ptrCharacterTurnOrderArray;
ptrCharacterTurnOrderArray.push_back(player_ptr);
ptrCharacterTurnOrderArray.push_back(enemy_ptr);


sort(ptrCharacterTurnOrderArray.begin(), ptrCharacterTurnOrderArray.end(), [&](const CharacterBase& Char1, const CharacterBase& Char2)
    {

        //First Argument Stays First (Return true)
        if (Char1.getCharacterInitiative() < Char2.getCharacterInitiative())
        {
            return true;
        }
        //First Argument goes Next (Swap) (Return false)
        if (Char1.getCharacterInitiative() > Char2.getCharacterInitiative())
        {
            return false;
        }

        if (Char1.getCharacterInitiative() == Char2.getCharacterInitiative())
        {
            engineFeedback = engineFeedback   " \n Error, matching initiative values found between "   Char1.getCharacterName()   " and "   Char2.getCharacterName();
        }

    });

//The order needs to be reversed so that the highest turn order appears first.
reverse(ptrCharacterTurnOrderArray.begin(), ptrCharacterTurnOrderArray.end());

TurnOrderArraySize = ptrCharacterTurnOrderArray.size();
engineFeedback = engineFeedback   "\nTurn Order Array Size: "   std::to_string(TurnOrderArraySize);
//Set Turn orders now and provide a readout of the values for each item within the character Turn Order Array.
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());
}

  }

CharacterBase::CharacterBase() {

  }
  
 double CharacterBase::getCharacterInitiative() const
 {
return cInitiative;
 }




   void CharacterBase::setCharacterInitiative(double sInitiative)
  {
    cInitiative = sInitiative;
     }

 //gets the Character's turn order
 int CharacterBase::getCharacterTurnOrder() const
  {
return cTurnOrder;
 }


   //sets the Character's turn order
 void CharacterBase::setCharacterTurnOrder(int sTurnOrder)
  {
cTurnOrder = sTurnOrder;
}

 double cInitiative;
 int cTurnOrder;

CodePudding user response:

The cause of the error is that your ordering function wants two references, but the vector contains pointers.
(You're also failing to return a value on one path; comparing two equivalent values must return false, but that's undefined behaviour and not a compilation error.)

You could fix that, but you have a whole lot of complicated code essentially just for determining which of two numbers is the greatest.
Here is one suggestion for simplifying it:

std::string turnFeedback(const CharacterBase& c)
{
    std::ofstream os;
    os << "\n Name: " << c.getCharacterName()
       << " Initiative: " << c.getCharacterInitiative()
       << " Player Character Flag: " << c.getIsPlayerCharacter()
       << "\nTurn Order: " << c.getCharacterTurnOrder();
    return os.str();
}

void EVoot::IRollTwoCombatantsPtrs(CharacterBase& PC, CharacterBase& Enemy) {
    auto pcValue = PC.getCharacterInitiative();
    auto enemyValue = Enemy.getCharacterInitiative();
    if (pcValue == enemyValue)
    {
        engineFeedback = engineFeedback   " \n Error, matching initiative values found between "
                         PC.getCharacterName()   " and "   Enemy.getCharacterName();                
    }
    else
    {
        auto pcWins = pcValue > enemyValue;
        CharacterBase& first = pcWins ? PC : Enemy;
        CharacterBase& second = pcWins ? Enemy : PC;
        first.setCharacterTurnOrder(1);
        second.setCharacterTurnOrder(2);
        engineFeedback  = turnFeedback(first);
        engineFeedback  = turnFeedback(second);
    }
}
  • Related