调用构造函数时出错

Sab*_*Sab 1 c++ constructor struct class

我试图从类构造函数调用结构的构造函数,但它抛出一个错误.我一直试图解决这个问题30分钟

结构体:

struct node
{
    int val;
    node* left;
    node* right;
    node(int x)
    {
        val = x;
        left = NULL;
        right = NULL;
    }

    ~node()
    {
        delete(left);
        delete(right);
    }
};
Run Code Online (Sandbox Code Playgroud)

类:

class Tree
{
    node* head;
    node list[1000];
    bool flag[1000] = {0};
public:
    Tree(int x)
    {
        head = new node(x);
    }
Run Code Online (Sandbox Code Playgroud)

main()方法:

int main()
{
    int a[] = {50,35,75,20,45,60,90,10,25,40,47,65,80,120,1,15,23,30,39,
46,49,82,110,21,24,48};
    Tree t(a[0]);
Run Code Online (Sandbox Code Playgroud)

我得到的错误是

错误日志:

In constructor 'Tree::Tree(int)':|
|37|error: no matching function for call to 'node::node()'|
|37|note: candidates are:|
|17|note: node::node(int)|
|17|note:   candidate expects 1 argument, 0 provided|
|12|note: node::node(const node&)|
|12|note:   candidate expects 1 argument, 0 provided|
Run Code Online (Sandbox Code Playgroud)

结构构造函数有一个参数,在类构造函数中我用一个参数调用但错误是抛出程序调用0参数.我不知道问题出在哪里.

谢谢.

4pi*_*ie0 5

node list[1000];
Run Code Online (Sandbox Code Playgroud)

是一组结构.作为数组元素的结构需要一个默认构造函数(或必须显式指定初始化程序,参见示例),因此错误.添加默认构造函数node.


C++标准n3337 § 12.6.1/3

[注意:如果T是没有默认构造函数的类类型,如果没有明确指定初始化程序,则对T(或其数组)类型的对象的任何声明都是错误的(参见12.6和8.5). - 结束说明]