关于结构体嵌套结构体
查看原帖
关于结构体嵌套结构体
528114
jjsnam楼主2022/8/2 21:21

跟解题思路没太大关系,AC代码

主要是我在写代码时遇到了这样一个问题:

我先定义了一个矩阵结构体,后来有用另一个结构体封装线段树,大概这样:

struct Matrix{
    int v[maxM][maxM];
    int L, C;

    Matrix(int l, int c, bool st = false){
        L = l, C = c;
        memset(v, 0, sizeof v);
        if (st){
            for (int i = 0; i < max(l, c); i ++){
                v[i][i] = 1;
            }
        }
    }

	/* ... */
};

struct Segment_Tree{
    struct Matrix M;
    struct Matrix lazy;
}tr[maxn << 2];

之后,编译器报错:

implicit default constructor for 'Segment_Tree' must explicitly initialize the member 'M' which does not have a default constructor.

似乎让我初始化定义的矩阵?所以我改成了这样:

struct Segment_Tree{
    struct Matrix M(1, 2);
    struct Matrix lazy(2, 2, true);
}tr[maxn << 2];

然后就。。。。

我懵逼了就。然后我就把初始化函数改成 void init(...) 了,就没有问题了。

就是想问问按我原来的写法应该怎么写,还是说不能?求大佬解答!

2022/8/2 21:21
加载中...