使用新操作器创建多个对象
Multiple Object creation using new operant
>我有一个模板类,我想使用 new 运算符创建该类的多个对象,但我似乎无法使其工作。这就是我尝试创建对象的方式
Penalty<GraphType, AltGraph> *penalty = new Penalty<GraphType, AltGraph>( G, AG, algTimestamp, maxNumDecisionEdges + offset)[5];
我得到的错误是
penalty.cpp:343:130: error: expected ‘,’ or ‘;’ before ‘[’ token
Penalty<GraphType, AltGraph> *penalty = new Penalty<GraphType, AltGraph>( G, AG, algTimestamp, maxNumDecisionEdges + offset)[5];
你能帮忙解决这个问题吗?
提前谢谢你
不能为数组的 5 个对象调用一次构造函数。
一种解决方案可能是创建一个指针数组,然后为循环中的每个项目调用 new:
Penalty<GraphType, AltGraph> **penalty = new Penalty<GraphType, AltGraph>*[5];
for (int i = 0; i < 5; i++)
penalty[i] = new Penalty<GraphType, AltGraph>( G, AG, algTimestamp, maxNumDecisionEdges + offset);
相关文章:
- 如何为模板化对象创建模板向量?VS正在投掷C3203
- 具有包含其他对象的类的对象创建顺序
- 为什么我们再次从结构对象创建结构变量?
- 将对象创建为全局/静态对象会崩溃,而本地对象不会崩溃
- 如何创建一个对象创建函数,该函数将由与其关联的名称调用?
- 如何为自定义模板对象创建专门的函数模板
- 对象创建错误的C++矢量
- 如何为Python Swigged C++对象创建和分配回调函数
- 在 C++ 中为 C 样式对象创建一个透明包装类
- 此类模板的对象创建如何工作?
- C++ 中的对象创建类型有什么区别?
- 未知大小的数组作为类成员,用于在运行时(对象创建时间)创建数组的对象
- 如何仅通过父类对象限制对象创建
- 在正确性或良好的代码结构方面,这种动态对象创建看起来如何
- 无法将类对象创建为另一个类的成员
- 如何从现有基类对象创建派生类对象
- 在 DTor 之前删除的静态对象创建的线程?
- C++:定义多个构造函数时的对象创建/销毁序列
- 从对象创建矢量包装器,该对象只允许使用索引访问向量
- 是否可以为CPP中的对象创建一组指针