【发布时间】:2020-04-17 15:29:33
【问题描述】:
我正在尝试创建一个 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 直接初始化向量?
【问题讨论】:
-
std::vector<CPoints> points(point1, point2);这和你想的不一样。查看文档。 -
std::vector<CPoints> points(NULL);-- 鉴于NULL为0,NULL绝对什么都不做。只需声明vector就足够了。 -
@PaulMcKenzie 将参数完全留空会导致
push_back()函数不起作用。它给出了一个expression must have a class type错误。 -
@BrentMB 所以你这样做了:
std::vector<CPoints> points();?好吧,您已经发现了“最令人头疼的解析”问题。这是一个名为points的函数的声明,它不接受任何参数,并返回一个std::vector<CPoints>。 -
@PaulMcKenzie 有趣的是,我以前从未听说过这种歧义。我会调查的!
标签: c++ vector initialization initializer-list