2016-12-15 38 views
-3
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; 
    } 
     } 
    } 
    } 
} 

我有一個類型爲spaces的數據類型的宇宙飛船的2D矢量。 我想添加項目對象到第一個太空船,但它給了我一個錯誤,說類太空船項目沒有一個名爲push_back的成員2d向量說類沒有名爲push_back的成員

這是這條線,我沒有看到它有什麼問題。

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

幫助表示讚賞。

編輯:如果你打算讓我失望,至少給我一個理由。我只是在問一個問題。

+1

'm_itemShips [i]'看上去是'Spaceship ',而不是'std :: vector'。 'Spaceship'可能有也可能沒有'push_back'方法。沒有[mcve]或猜測就不能說更多。關閉主題,請使用[成員初始化程序列表](http://en.cppreference.com/w/cpp/language/initializer_list)。 – user4581301

+0

飛船是一個模板類。它沒有push_back方法,但ManageSupply類創建了一個向量類型spaceship ,所以它應該有權訪問push_back。我不確定 –

+0

「所以它應該有權訪問push_back」什麼push_back?你說沒有一個,那是你的問題。 – mascoj

回答

0

m_itemShips是(假定)std::vector< Spaceship <Item> >m_itemsstd::vector<Item>。 (並且您沒有提供關於SpaceShipItem的信息)。

這意味着m_itemShips[i].push_back(m_items[itemCount])要求SpaceShip<Item>有一個名爲push_back()的成員函數,它可以接受Item

如果SpaceShip<Item>沒有這樣的push_back()成員函數,它解釋了您的錯誤消息。

重要的是,SpaceShip<Item>並不奇蹟般地獲得名爲push_back()的方法,即使它具有類型爲std::vector<Item>的數據成員。如果你想要這樣一個成員函數,你必須定義它。