新增[size]对象(args ..)在数组new [-fpermissive]中添加圆括号初始化器
new[size] object(args ... ) , GCC parenthesized initializer in array new [-fpermissive]
我的代码有一个问题,我真的不明白。
我使用«gcc version 4.7.2 (Ubuntu/Linaro 4.7.2-2ubuntu1)»
编辑:我用这一行来编译
g++ -g -std=c++0x -o "GeneticEngine.o" -c "GeneticEngine.cpp"
错误如下:
GeneticEngine.tpl:16:5: erreur: parenthesized initializer in array new [-fpermissive]
这是我的(最小化)代码:
GeneticEngine.hpp
#include "GeneticThread.hpp"
template <class T>
class GeneticEngine
{
public:
template <typename ... Args>
GeneticEngine(int nb_threads,float taux_mut,int tranche_mut,std::string filename,int pop_size,Args& ... args);
/* Other code */
private:
GeneticThread<T>* islands; /* Cause of error */
int size;
};
#include "GeneticEngine.tpl"
GeneticEngine.tpl
template <class T>
template <typename ... Args>
GeneticEngine<T>::GeneticEngine(int nb_threads,float taux_mut,int tranche_mut,std::string filename,int pop_size,Args& ... args) : size(nb_threads)
{
/*next line is 16 : Error */
islands = new GeneticThread<T>[size](taux_mut,tranche_mut,filename,pop_size/nb_threads,std::forward<Args>(args)...);
};
GeneticThread.hpp
template <class T>
class GeneticThread
{
public:
template <typename ... Args>
GeneticThread(float taux_mut,int tranche_mut,std::string filename,int pop_size,Args& ... args)
{ /* code ... */ };
/* Other code */
};
我读过这个(使用模板时初始化数组),但它不完全相同。
如果你有一个想法来修复它,而不需要:[最后我使用这个:/]
GeneticThread<T>** islands;
islands = new GeneticThread<T>*[size];
for(int i=0;i<size;++i)
islands[i] = new GeneticThread<T>(taux_mut,tranche_mut,filename,pop_size/nb_threads,std::forward<Args>(args)...);
我想有:
GeneticThread<T>* islands;
有办法吗??
我试一试:
islands = new (GeneticThread<T>[size](taux_mut,tranche_mut,filename,pop_size/nb_threads,std::forward<Args>(args)...));
和
islands = new GeneticThread<T>(taux_mut,tranche_mut,filename,pop_size/nb_threads,std::forward<Args>(args)...)[size];
但它不工作。
谢谢。
不能为new
. ly分配的数组使用非默认构造函数。
不像这样使用new
,而是使用vector
并将一个正确构造的对象传递给vector
构造函数!即使你不需要调整内存大小,vector
也会确保你的内存被妥善管理,不会泄露。
std::vector<GeneticThread<T>*> islands;
:
GeneticEngine<T>::GeneticEngine(int nb_threads,float taux_mut,int tranche_mut,std::string filename,int pop_size,Args& ... args)
: size(nb_threads), islands(size, GeneticThread<T>(taux_mut,tranche_mut,filename,pop_size/nb_threads,std::forward<Args>(args)...))
{
};
相关文章:
- 将成员变量添加到共享库中的类中,不会破坏二进制兼容性吗
- 如何在C++中从两个单独的for循环中添加两个数组
- POCO::PostgreSQL:如何将std::vector支持添加到`Binder::bind`
- 如何仅为一个函数添加延迟
- 如何防止 c++ 在从浮点型转换为双精度型(不适用于 IO)时添加额外的小数?
- 使用std::transform将一个范围的元素添加到另一个范围中
- 如何将更多文件夹添加到c++include路径
- 如何将元素添加到数组的线程安全函数?
- QT通过C++添加映射QML项目
- 如何将点击的信号和插槽添加到qt中的自定义按钮中
- 如何使用重载的相等(==)运算符向测试用例添加描述
- 为什么Mat类的两个对象可以在不重载运算符+的情况下添加
- 如何防止clang格式在流运算符调用之间添加换行符<<
- 只能向C++添加一定数量的字符
- Qt和C++:将QLineEdit添加到QTabWidget中
- 将QIcon添加到QTableView单元格
- 为什么我必须在C++中添加一个赋值符号来声明一个数组
- 为什么除非添加括号,否则构造函数上的模板替换会失败?
- 将图像添加到资源文件夹UWP C++
- 新增[size]对象(args ..)在数组new [-fpermissive]中添加圆括号初始化器