2d vector says class does not have member named push_back

596 views Asked by At
ManageShips::ManageShips(vector< Spaceship <Item> > itemShip,
                     vector< Spaceship <Person> > personShip,
                     vector< Item > itemCargo,
                     vector< Person > personCargo){

  m_itemShips = itemShip;
  m_personShips = personShip;
  m_items = itemCargo;
  m_person = personCargo;

}

void ManageShips::LoadItemShip(){

  int numberItemShips = m_itemShips.size();

  int numberOfItems = m_items.size();

  int itemCount = 0;

  for (int i = 0 ; i < numberItemShips ; i++){

    int shipFull = 0;

    double shipWeight = 0;


    while ( shipFull == 0){

      while (itemCount < numberOfItems){

    if ((m_items[itemCount].GetWeight() + shipWeight ) > m_itemShips[i].GetCapacity()){
      shipFull = 1;
    }
    else {
      m_itemShips[i].push_back(m_items[itemCount]);
      shipWeight = m_items[itemCount].GetWeight() + shipWeight;
      itemCount = itemCount + 1;
    }
      }
    }
  }
}

I have a 2d vector of type spaceships which holds data of type items. I am trying to add item objects into the first spaceship but it gives me an error saying class spaceship item does not have a member named push_back

it's this line and I dont see what's wrong with it.

  m_itemShips[i].push_back(m_items[itemCount]);

help is appreciated.

Edit: if youre gonna downvote me atleast give me a reason please. I am just asking a question.

1

There are 1 answers

0
Peter On

m_itemShips is a (presumably) a std::vector< Spaceship <Item> > and m_items is a std::vector< Item >. (and you have given no information about what SpaceShip or Item are).

This means that m_itemShips[i].push_back(m_items[itemCount]) requires SpaceShip<Item> to have a member function named push_back() that can accept an Item.

If SpaceShip<Item> does not have such a push_back() member function, that explains your error message.

Importantly, SpaceShip<Item> does not magically acquire a method named push_back() even if it has a data member of type std::vector<Item>. If you want such a member function, you have to define it.