1

I want initialize a vector of vector of bool inside my costructor.

This is my class:

class MyClass{
public:
    MyClass(const OtherClass&g):
        g(g), count(g.node_count(), std::vector<bool>(16))){}


private:
    const OtherClass&g;
    std::vector<std::vector<bool>>count;
};

but when I try to initialize count I obtain this error:

error: no match for call to ‘(std::vector<std::vector<bool> >) (int)’
2
  • Unrelated, try to avoid vector<bool>, it's a hybrid monster. You can use std::bitset<> instead. Commented Dec 6, 2016 at 14:51
  • strange why use a vector if you know that you need 16 bool ? and why const OtherClass& ? Trouble incoming with this design. When you give a reference of an object(A) to an other object(B) this object(A) should belong to him(B) so why const ? (maybe you don't want to modify him, so it's ok) Commented Dec 6, 2016 at 14:53

2 Answers 2

3

You want to use fill constructor. If you don't use c++ 11 you need to specify the default value for elements in the vector count(g.node_count(), std::vector<bool>(16, true))

Sign up to request clarification or add additional context in comments.

Comments

0

First of all I want to ask if you know that vector<bool> is a special type of vector with a bit different implementation because of optimisation, which can cause a bit different behaviour in some cases.

If you want to use it, you must pass to constructor vector<bool>(16, true) filling it with 16 true values.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.