문제

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);
도움이 되었습니까?

해결책

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.

다른 팁

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));

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top