我正在尝试创建一个typedef的向量。每当我尝试使用这些typedef之一初始化向量时,都会产生no instance of constructor
错误。
typedef定义如下:
typedef palam::geometry::Pt2<uint16_t> CPoints;
并且我正在尝试像这样初始化向量:
CPoints point1(10, 15);
CPoints point2(15, 20);
std::vector<CPoints> points(point1, point2);
但这不起作用。我可以通过使用NULL
值初始化向量,然后使用push_back()
函数来解决此问题,例如
CPoints point1(10, 15);
CPoints point2(15, 20);
std::vector<CPoints> points(NULL);
points.push_back(point1);
points.push_back(point2);
这种解决方法似乎有些混乱,我相信必须有更好的方法来解决此问题。有谁知道为什么我无法使用typedefs直接初始化向量?
答案 0 :(得分:3)
此代码段:
std::vector<CPoints> points(point1, point2);
调用vector
constructor并接受2个参数。如果要使用多个元素初始化vector
,请使用{}
,如下所示:
std::vector<CPoints> points {point1, point2};
这将调用重载数字9,该数字带有一个初始化程序列表。
答案 1 :(得分:0)
使用此记录
std::vector<CPoints> points = { point1, point2 };
或者这个
std::vector<CPoints> points { point1, point2 };
或这个
std::vector<CPoints> points( { point1, point2 } );
如果要同时向一个向量提供多个对象,则使用初始化列表。
否则,编译器将尝试应用这些构造函数之一
vector(size_type n, const T& value, const Allocator& = Allocator());
template <class InputIterator>
vector(InputIterator first, InputIterator last,
const Allocator& = Allocator());
对于声明中的指定参数无效
std::vector<CPoints> points(point1, point2);