相关疑难解决方法(0)

类型是否需要默认构造函数才能声明它的数组?

我注意到,当您声明一个数组时,必须使用默认构造函数.是对的吗?有什么例外吗?

例如,

struct Foo{
Foo(int i  ) {}
};

int main () {
    Foo f[5];
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

上面的代码不能编译.

c++ arrays constructor

3
推荐指数
2
解决办法
504
查看次数

调用构造函数时出错

我试图从类构造函数调用结构的构造函数,但它抛出一个错误.我一直试图解决这个问题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)| …
Run Code Online (Sandbox Code Playgroud)

c++ constructor struct class

1
推荐指数
1
解决办法
102
查看次数

标签 统计

c++ ×2

constructor ×2

arrays ×1

class ×1

struct ×1