push_back指针指针的向量

时间:2014-06-13 10:03:06

标签: c++

std::vector<BPatch_point *> *points;
points = functions[0]->findPoint(BPatch_entry);
if ((*points).size() == 0)
{
  cout << "Failed to get the points" << endl;
}
std::vector<ldframework::Point *> *new_points;
for(unsigned int i = 0; i < points->size(); i++ )
{
  ldframework::Point *pt1 = new ldframework::PointImpl((*points[0]));
  new_points->push_back(pt1);
}

PointImpl构造函数是:

ldframework::PointImpl::PointImpl(const BPatch_point&* po)
{
    Bpatch_point *_bpoint=new BPatch_point(*po);     
}

WHile编译时遇到以下错误。你能帮忙吗

>BfunctionImpl.cpp: In member function âvirtual const std::vector<ldframework::Point*, std::allocator<ldframework::Point*> >* ldframework::BfunctionImpl::find_Point(ldframework::locClass)â:
  

BfunctionImpl.cpp:86:错误:期望的类型说明符   BfunctionImpl.cpp:86:错误:在初始化时无法将âint*â转换为âldframework:: Point *â   BfunctionImpl.cpp:86:错误:预期â,â或•

86行号码是

ldframework::Point *pt1 = new ldframework::PointImpl((*points[0]));

1 个答案:

答案 0 :(得分:0)

查看第86行的(*points[0])部分。

由于points是一个指向向量的指针,而数组下标operator []的优先级高于一元间接运算符*,因此它被解析为*(*(points+0))

您可以尝试将其替换为(*points)[0]或甚至可能:(*points)[i]

此外,new_points是一个指向向量的指针,在使用之前,该向量尚未分配给指向有效对象的指针。

此外,我发现你使用了很多指针,这会导致额外的间接性,减少局部性,并且会给你带来清理或导致内存泄漏的负担。 new_points必须是指向ldframework::Point的指针的向量吗?您可以使用std::unique_ptr<ldframework::Point>向量来避免清理负担,但您也可以考虑:

std::vector<ldframework::PointImpl> *new_points =
  new std::vector<ldframework::PointImpl>;
for(unsigned int i = 0; i < points->size(); i++ )
{
  new_points->push_back( ldframework::PointImpl((*points)[i]) );
}

甚至更好:

std::vector<ldframework::PointImpl> new_points;
for(unsigned int i = 0; i < points->size(); i++ )
{
  new_points.push_back( ldframework::PointImpl((*points)[i]) );
}