Question

So in my body code I make an iterator to work through a list of pointers to Point objects and I need to be able to pass that pointer to the set_point_one function.

list<Point*>::iterator it = on.begin();
l->set_point_one(*it);

The set_point_one function is overloaded like this:

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

When I run this code, I get the error:

./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); }         ^

I've tried playing with dereferencing a bit with no luck. Is there something obvious I'm missing or is the only way for me to get it working to further overload the function so it also explicitly takes a pointer to the object?

Thanks in advance,

Max

Was it helpful?

Solution

*it dereferences the iterator, returning the object, it points. In your case - Point*.

As your function takes Point&, you need to dereference it one more time. Meaning

// not mandatory -v---v
l->set_point_one(*(*it));

OTHER TIPS

Hope the following sample help :

using namespace std ;

void test(int* &p)
{
    cout << *p << endl ;
}

int main()
{
    std::list<int*> lp ;
    int *p1,*p2 ;
    p1 = new int(100201)  ;
    p2 = new int(12345)  ;

    lp.push_back(p1) ;
    lp.push_back(p2) ;
    std::list<int*>::iterator it = lp.begin();
    test(*it) ;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top