CodeModeOn
CodeModeOn

Reputation: 111

Could not convert from brace-enclosed initializer list

I am trying to compile the code below but I keep running into the error could not convert '{{1, 2}, {5, 6}}' from '<brace-enclosed initializer list>' to 'Class1'. I am compiling the code in -std=c++11. Is my initialization wrong?

class Class1
{
public:
    vector<vector<int> > a;
    Class1(vector<vector<int> > p)
    {

        for(int i = 0; i < 2; i++)
            for(int j = 0; j < 2; j++)
                a[i][j] = p[i][j];
    }
};

int main()
{
    Class1 ClassValue =   {{ 1, 2, },{ 5, 6 } };
    return 0;
}

Upvotes: 1

Views: 3365

Answers (1)

bebidek
bebidek

Reputation: 592

Firstly, change your line to:

Class1 ClassValue ({{ 1, 2 },{ 5, 6 } });

Second thing is your constructor is invalid, as you write to non-existing memory. Instead, use:

Class1(vector<vector<int> > p):a(p){}

EDIT AFTER COMMENT: This one will be better (faster), as you don't copy second time.

Class1(vector<vector<int> > p):a(move(p)){}

Upvotes: 1

Related Questions