【发布时间】:2021-09-10 10:32:07
【问题描述】:
我编写了一个包含对象数组的类,每个对象都希望在其构造函数中指向一个 c-struct 的指针:
这里有一些代码:
class TheOtherClass
{
private:
SomeCStruct* m_pCStruct;
int m_ObjIdx;
public:
TheOtherClass::TheOtherClass(SomeCStruct* pCStruct, int ClassIdx)
: m_pCStruct(pCStruct),
m_ObjIdx(ClassIdx)
{}
}
class MyClass
{
private:
TheOtherClass m_objects[4];
SomeCStruct* m_pMyCStruct;
public:
MyClass::MyClass(SomeCStruct* pCStruct)
: m_pMyCStruct(pCStruct)
{
m_objects[4] = {TheOtherClass(&pCStruct, 1), TheOtherClass(&pCStruct, 2),
TheOtherClass(&pCStruct,3), TheOtherClass(&pCStruct, 4)};
}
我实际上想在构造函数的初始化列表中初始化 MyClass 的成员对象,但我正在阅读这不可能吗? 因此我尝试了上述方法,但我总是收到错误:“TheOtherClass”:没有合适的默认构造函数可用。
另外要提的是,我正在运行的编译器是最先进的 C++03 编译器;-)(它是 DSP 的代码,TI 有点不想更新编译器)
任何帮助将不胜感激!
【问题讨论】:
-
private: TheOtherClass m_objects[4];-- 您的错误不需要复制所有这些代码:int main() { TheOtherClass t; }-- 仅此一项是无法完成的,这都是因为没有默认构造函数。
标签: c++ pointers struct constructor