0

I'm trying to initialize a vector holding Node objects with a specific size. The code:

std::vector<Node> vertices(500);

produces the following errors:

In constructor ‘std::vector<_Tp, _Alloc>::vector(std::vector<_Tp, _Alloc>::size_type, const value_type&, const allocator_type&) [with _Tp = Node; _Alloc = std::allocator<Node>; std::vector<_Tp, _Alloc>::size_type = long unsigned int; std::vector<_Tp,         _Alloc>::value_type = Node; std::vector<_Tp, _Alloc>::allocator_type =   std::allocator<Node>]’:
test.cpp:47:36: error: no matching function for call to ‘Node::Node()’
    std::vector<Node> vertices(500);
                                ^
test.cpp:47:36: note: candidates are:
test.cpp:13:3: note: Node::Node(unsigned int)
   Node(unsigned int label) : m_label(label), degree(0) {}
   ^
test.cpp:13:3: note:   candidate expects 1 argument, 0 provided
test.cpp:7:7: note: Node::Node(const Node&)
 class Node {
       ^
test.cpp:7:7: note:   candidate expects 1 argument, 0 provided
test.cpp:47:36: note:   when instantiating default argument for call to std::vector<_Tp,     _Alloc>::vector(std::vector<_Tp, _Alloc>::size_type, const value_type&, const  allocator_type&) [with _Tp = Node; _Alloc = std::allocator<Node>; std::vector<_Tp,  _Alloc>::size_type = long unsigned int; std::vector<_Tp, _Alloc>::value_type = Node;    std::vector<_Tp, _Alloc>::allocator_type = std::allocator<Node>]
    std::vector<Node> vertices(500);
1

3 Answers 3

2

Your call requires that Node objects can be created. The first line of your error message says, that it does not have a default constructor.

Therefore the compiler has no idea how to create the 500 objects you requested.

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

Comments

1

Since Node evidently has no default constructor, you cannot default-construct 500 of them.

You'll have to provide constructor arguments for each from the get-go, by instantiating each element from a placeholder:

std::vector<Node> vertices(500, Node(args));

Comments

1

According to vector constructor reference, compiler is telling you it can't find the default constructor for this class.

To solve this issue, you will need to implement a default constructor for the class Node or to provide an instance of Node to be copied 500 times.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.