Max*_*ahm 2 c++ pointers iterator pass-by-reference
所以在我的正文代码中,我创建了一个迭代器来处理指向Point对象的指针列表,我需要能够将该指针传递给set_point_one函数.
list<Point*>::iterator it = on.begin();
l->set_point_one(*it);
Run Code Online (Sandbox Code Playgroud)
set_point_one函数重载如下:
void set_point_one(const double x, const double y, const double z) { one_.set_xyz(x, y, z); }
void set_point_one(Point &p) { one_.set_xyz(p.get_x(), p.get_y(), p.get_z()); } //trying to get it to use this one
Run Code Online (Sandbox Code Playgroud)
当我运行此代码时,我收到错误:
./facet.cc:79:20: error: no matching member function for call to 'set_point_one'
l->set_point_one(*it); //set both end points of the line to the point
~~~^~~~~~~~~~~~~
./line.cc:21:10: note: candidate function not viable: no known conversion from 'value_type' (aka 'Point *') to 'Point &' for 1st argument; dereference the argument with
*
void set_point_one(Point &p) { one_.set_xyz(p.get_x(), p.get_y(), p.get_z()); }
^
./line.cc:20:10: note: candidate function not viable: requires 3 arguments, but 1 was provided
void set_point_one(const double x, const double y, const double z) { one_.set_xyz(x, y, z); } ^
Run Code Online (Sandbox Code Playgroud)
我试过玩一点没有运气的解除引用.有没有明显的东西我缺少或是我唯一的方法让它进一步重载函数,所以它也明确地采取指向对象的指针?
提前致谢,
马克斯
*it取消引用迭代器,返回对象,它指向.在你的情况下 - Point*.
随着您的功能需要Point&,您需要再次取消引用它.含义
// not mandatory -v---v
l->set_point_one(*(*it));
Run Code Online (Sandbox Code Playgroud)