这是我面临的一个奇怪的问题 - 考虑到我生锈的C ++技能,可能是超基本的东西,但我仍然感到困惑:
unsigned long long
- 让我们称之为arr
我的班级界面:
typedef unsigned long long U64;
class DQClass
{
public:
DQClass (void);
virtual ~DQClass (void);
U64 arr[12];
};
现在关于实施......
测试1(此作品):
DQClass::DQClass (void)
{
this->arr[0] = 0x8100000000000000ULL;
this->arr[1] = 0x4200000000000000ULL;
// and so on..
}
测试2(不是):
DQClass::DQClass (void)
{
this->arr =
{
0x8100000000000000ULL,
0x4200000000000000ULL,
0x2400000000000000ULL,
0x1000000000000000ULL,
0x0800000000000000ULL,
0x00FF000000000000ULL,
FLIPV(0x8100000000000000ULL),
FLIPV(0x4200000000000000ULL),
FLIPV(0x2400000000000000ULL),
FLIPV(0x1000000000000000ULL),
FLIPV(0x0800000000000000ULL),
FLIPV(0x00FF000000000000ULL)
};
}
错误:
dqclass.cpp: In constructor ‘DQClass::DQClass()’:
dqclass.cpp:28: error: expected primary-expression before ‘{’ token
dqclass.cpp:28: error: expected `;' before ‘{’ token
为什么这不起作用?它不应该以与例如相同的方式工作。 U64 someArr[12] = {0,1,2,3,4,5,6,7,8,9,10,11}
会吗?
有什么想法吗?
答案 0 :(得分:6)
不能像那样(或任何其他方式)分配数组,只能初始化:
// sorry for bad formatting
DQClass::DQClass (void)
: arr(
{
0x8100000000000000ULL,
0x4200000000000000ULL,
0x2400000000000000ULL,
0x1000000000000000ULL,
0x0800000000000000ULL,
0x00FF000000000000ULL,
FLIPV(0x8100000000000000ULL),
FLIPV(0x4200000000000000ULL),
FLIPV(0x2400000000000000ULL),
FLIPV(0x1000000000000000ULL),
FLIPV(0x0800000000000000ULL),
FLIPV(0x00FF000000000000ULL)
}) {
}
使用构造函数初始化列表。
您也可以使用std::array
:
std::array<U64, 12> arr;
// ...
this->arr =
{{
0x8100000000000000ULL,
0x4200000000000000ULL,
0x2400000000000000ULL,
0x1000000000000000ULL,
0x0800000000000000ULL,
0x00FF000000000000ULL,
FLIPV(0x8100000000000000ULL),
FLIPV(0x4200000000000000ULL),
FLIPV(0x2400000000000000ULL),
FLIPV(0x1000000000000000ULL),
FLIPV(0x0800000000000000ULL),
FLIPV(0x00FF000000000000ULL)
}};