首页 > 解决方案 > 编译器无法识别构造函数,需要参数并识别零

问题描述

我正在尝试将数据组织成二叉树,并创建了一个结构来更好地组织数据。但是,每次我尝试运行我的代码时,我的编译器都会出现此错误消息:

BinaryTree.cpp:41:37: error: no matching function for call to 'person::person()'
  node (person i, node * l, node * r){
                                     ^
BinaryTree.cpp:14:2: note: candidate: 'person::person(int, int, std::__cxx11::string, std::__cxx11::string, int)'
  person(int ss, int bd, string fn, string ln, int zc) {
  ^~~~~~
BinaryTree.cpp:14:2: note:   candidate expects 5 arguments, 0 provided
BinaryTree.cpp:10:8: note: candidate: 'person::person(const person&)'
 struct person {
        ^~~~~~
BinaryTree.cpp:10:8: note:   candidate expects 1 argument, 0 provided
BinaryTree.cpp:10:8: note: candidate: 'person::person(person&&)'
BinaryTree.cpp:10:8: note:   candidate expects 1 argument, 0 provided

它所指的代码行是我创建的这些结构。我以前使用过 person 结构,它工作得很好,所以我对我的错误感到困惑。

struct person {
    int socialSecurity, birthDate, zipCode;
    string firstName, lastName;
person(int ss, int bd, string fn, string ln, int zc) {
    socialSecurity = ss;
    birthDate = bd;
    firstName = fn;
    lastName = ln;
    zipCode = zc;
}
};

struct node {
    person info;
    node * left, * right;

node (person i, node * l, node * r){
    info = i;
    left = l;
    right = r;
    }
};

我对此有点陌生,所以如果您需要更多代码来帮助我找出问题所在,我会尽力提供。

标签: c++unix

解决方案


由于您定义了非默认构造函数,编译器不会生成默认构造函数,请参阅https://en.cppreference.com/w/cpp/language/default_constructor了解更多信息。要解决这个问题,请参阅未生成默认构造函数?.


推荐阅读