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

前端 未结 5 747
星月不相逢
星月不相逢 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条回答
  •  被撕碎了的回忆
    2020-12-02 09:54

    You cannot insert a class into a vector, you can insert an object (provided that it is of the proper type or convertible) of a class though.

    If the type Player has a default constructor, you can create a temporary object by doing Player(), and that should work for your case:

    vectorOfGamers.push_back(Player());
    

提交回复
热议问题