【发布时间】:2014-10-15 22:58:18
【问题描述】:
简而言之,当该类未定义复制构造函数且没有默认构造函数时,我想将元素添加到类元素向量中。这是一个最小的示例(感谢 juanchopanza 修复了我的示例):
class MyType {
public:
MyType(int height, int width);
MyType() = delete;
}
#include <vector>
int some_function() {
// Define the vector.
std::vector<MyType> array;
// Define an element.
MyType element(1,2);
// Now, how do I add an element to the vector?
//array.resize(1); // NO DEFAULT CONSTRUCTOR SO THIS WON'T WORK.
//array.push_back(element); // NO COPY CONSTRUCTOR SO THIS WON'T WORK.
}
我最好的猜测是,我不能将向量与类一起使用,除非它具有复制构造函数或默认构造函数。寻找验证或启发。
【问题讨论】:
-
你有没有不小心把
MyType复制成可构造的?