Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/154.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用新operant创建多个对象_C++ - Fatal编程技术网

C++ 使用新operant创建多个对象

C++ 使用新operant创建多个对象,c++,C++,我有一个模板类,我想使用新操作符创建该类的多个对象,但我似乎无法使其工作。 这就是我试图创建对象的方式 Penalty<GraphType, AltGraph> *penalty = new Penalty<GraphType, AltGraph>( G, AG, algTimestamp, maxNumDecisionEdges + offset)[5]; 惩罚*惩罚=新的惩罚(G、AG、algTimestamp、MaxNumDecisionEdge+偏移量)[5]

我有一个模板类,我想使用新操作符创建该类的多个对象,但我似乎无法使其工作。 这就是我试图创建对象的方式

Penalty<GraphType, AltGraph> *penalty = new  Penalty<GraphType, AltGraph>( G, AG, algTimestamp, maxNumDecisionEdges + offset)[5];
惩罚*惩罚=新的惩罚(G、AG、algTimestamp、MaxNumDecisionEdge+偏移量)[5];
我得到的错误是

penalty.cpp:343:130: error: expected ‘,’ or ‘;’ before ‘[’ token
     Penalty<GraphType, AltGraph> *penalty = new  Penalty<GraphType, AltGraph>( G, AG, algTimestamp, maxNumDecisionEdges + offset)[5];
惩罚。cpp:343:130:错误:应为“,”或“;”在“[”标记之前
惩罚*惩罚=新惩罚(G、AG、algTimestamp、MaxNumDecisionEdge+偏移量)[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);
惩罚**惩罚=新惩罚*[5];
对于(int i=0;i<5;i++)
惩罚[i]=新惩罚(G、AG、algTimestamp、MaxNumDecisionEdge+偏移);
只需使用
std::vector