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