关于构造函数段错误的求助
  • 板块学术版
  • 楼主ATZdhjeb
  • 当前回复9
  • 已保存回复9
  • 发布时间2023/6/26 13:28
  • 上次更新2023/11/3 12:23:54
查看原帖
关于构造函数段错误的求助
483317
ATZdhjeb楼主2023/6/26 13:28

RT,我在做一道题时写了如下结构体:

struct Binary_indexed_array {
    int t[2000010],n;
	
    Binary_indexed_array(const int& a = 0) {
        n = a;
        memset(t,0,sizeof(t));
    }
	
    inline void add(const int& u,const int& k) {
        if (u < 1) return;
        for (int i = u; i <= n; i += lowbit(i)) t[i] += k;
    }
	
    inline int query(const int& u) {
        int ans = 0;
        for (int i = u; i >= 1; i -= lowbit(i)) ans += t[i];
        return ans;
    }
}st,nd;

然后在主函数调用其构造函数:

int main() {
    n = input();
    c = input();
    q = input();
    st = Binary_indexed_array(c);
    nd = Binary_indexed_array(c);
    
    ... //省略其它代码,因为确定是上面5行错了
    
    return 0;
}

然后就段错误。而且段错误的发生在n = input()执行之前,因为我根本没来得及输入。而注释掉两行调用构造函数之后运行就不会出错。

虽然我也知道并不一定要用这种方式实现,但是我还是想问一下这样写哪里错了 QWQ。

2023/6/26 13:28
加载中...