How can I create objects while adding them into a vector?

前端 未结 5 746
星月不相逢
星月不相逢 2020-12-02 09:13

I have a C++ vector. I want the vector to hold a variable number of objects.

Visual Studio 2012 is giving me an error:

Error: type name is not all         


        
5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-02 09:33

    // create a vector of unknown players.
    std::vector players;
    
    // resize said vector to only contain 6 players.
    players.resize(6);
    

    Values are always initialized, so a vector of 6 players is a vector of 6 valid player objects.

    As for the second part, you need to use pointers. Instantiating c++ interface as a child class

提交回复
热议问题