本文介绍了在结构中初始化数组时遇到问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
class CRA_Account {
int tax[4];
double refund[4];
int SIN;
public:
CRA_Account();
}
CRA_Account::CRA_Account() {
SIN = 0;
tax[4] = { 0 };
refund[4] = { 0 };
}
当我在main中创建一个对象时,它将SIN设置为0,但不会对数组做同样的事情.有人可以帮忙吗?
When I create a object in main it'll set the SIN to 0 but won't do the same to the arrays. Can someone help why?
推荐答案
tax [4] = {0};
在许多级别上都是错误的.初始化班级的一种方法:
tax[4] = { 0 };
is wrong at many levels..One way to initlizie your class:
CRA_Account::CRA_Account():
tax{0,0,0,0},
refund{0,0,0,0},
SIN{0}{
}
尝试看看 std :: array
这篇关于在结构中初始化数组时遇到问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!