为什么我得到“候选构造函数不可行”?

时间:2018-12-02 19:03:21

标签: c++

#include <stdio.h>
#include <vector>
#include <iostream>
#include <algorithm>

using namespace std;

struct Node1 {
    unsigned int vertex;
    unsigned int representative;
    Node1(unsigned int Vert, unsigned int Rep) : vertex(Vert), representative(Rep) {}
};

class Graph{
    vector<Node1> nodes;
public:

    void findComponents() {
        nodes.emplace_back(1, 1);
        nodes.resize(1);
//        nodes.resize(newSize);
    }
};

int main(){
    Graph g;
    g.findComponents();
}

我收到大量奇怪的构建错误,主要由“候选构造函数不可行”和“在实例化成员函数'std :: __ 1 :: vector> :: resize'中请求她”组成

1 个答案:

答案 0 :(得分:2)

要使用下面在代码中使用的vector::resize()重载,T必须满足MoveInsertable和DefaultInsertable的要求。

void resize( size_type count );

DefaultInsertable表示该类型的实例可以就地默认构造

因此,您需要的是Node1的默认构造函数。为此,您可以这样做:

Node1() = default;

或在现有的构造函数中为VertRep指定默认值,如下所示:

Node1(unsigned int Vert = 0, unsigned int Rep = 0) : vertex(Vert), representative(Rep) {}
相关问题