Search code examples
c++vectorconstructorsfml

initalizing array of vectors in constructor in sfml


I use a SFML library for graphics and other stuff,such as vectors. In my Brain class I try to do something like:

class Brain{

Brain(int size){
Vector2f directions[size];
}
}

But it throws an error saying it must evaluate to a constant. I tried all sorts of things but I can't get it to compile properly. Anyone knows why this happens and how can I fix it?


Solution

  • As suggested in the comments in C++ the size of an array must be known at compile time... if you need dynamic containers you can use std::vector.

    class Brain {
    public:
        Brain(int size) : _directions{size}
        {
        }
    
    private:
        vector<Vector2d> _directions;
    };
    

    Don't forget public and private access to your class... By default everything is private on a class so in your snippet the contructor of the class is private!