vector as a Data Member in C++

后端 未结 3 1024
孤城傲影
孤城傲影 2021-01-13 04:21

In C++, how do I include a 101 elements vector as a data member in my class? I\'m doing the following, but it doesn\'t seem to be working:

private:
    std::         


        
3条回答
  •  梦毁少年i
    2021-01-13 04:41

    You can't use the normal construction syntax to construct an object in the class definition. However, you can use uniform initialization syntax:

    #include 
    class C {
        std::vector integers{ 101 };
    };
    

    If you need to use C++03, you have to constructor your vector from a member initializer list instead:

    C::C(): integers(101) { /* ... */ }
    

提交回复
热议问题