Search code examples
c++vectorconstructorinitializationassign

Initialize a 2D vector inside constructor of class


I want to initialize a 2D array in the constructor of a class but the compiler gives me this error "call of an object of a class type without appropriate operator() or conversion functions to pointer-to-function type".

class Matrix
{

public:
    Matrix() {};
    Matrix(size_t x, size_t y) { a(x, vector<int>(y , 0 )); }
private:
    vector<vector<int>> a; 


};

Solution

  • Firstly, as mentioned, this:

    Matrix(size_t x, size_t y)
    {
       a(x, vector<int>(y , 0 ));
    }
    

    is not how you initialise a member. To initialise a member, we use the member initializer list, like so:

    Matrix(size_t x, size_t y)
       : a(x, vector<int>(y , 0 ))
    {}
    

    The colonic syntax is a specific place for listing initialisations. Nothing in the constructor body is an initialisation. When you don't put an initialiser for a member, it is default-initialised. Then the code in your constructor's body is run. As we know, the body of a function consists of statements that are executed.

    Now, when you perform the statement <name>(<thing>, <other thing>), that is a function call. If <name> is not a function but a class instance, then the function call operator operator() is looked up instead. If there isn't one (or it doesn't match the arguments), then compilation fails in the way that you've observed.

    Could the language have been designed so that any mention of a member, inside the constructor body, in this form, were treated as an initialisation? Probably. But why? Now you'd have no unambiguous way to perform function-calls instead, and the order in which things happen during construction would be unclear. It's much better the way it is now.