lundi 17 décembre 2018

Superclass method with argument list

I am new to C++ and I am trying to develop a 2D simulation on which every represented element is a subclass of Point. I have several a list for each subclass (listA, listB...).

I am trying to implement a function that finds the closest point to the current point, for example, which B instance is the closest to the current A instance. It makes sense to me to put this function in the Point class since it may be used by each subclass, but since my program uses lists of specialized items (std::list<A> instead of std::list<Point>), I get an error with the following code:

class Point 
{
    Point* findClosest(std::list<Point*>&);
};

class A : public Point
{
    void update(std::list<B*>&);
};

class B : public Point
{
};

void A::update(std::list<B*>& blist)
{
    A* closest = this->findClosest(blist); //bug
    // do something...
}

Error message:

error: no matching function for call to ‘A::findClosest(std::__cxx11::list<A*>&)’

note: no known conversion for argument 1 from ‘std::__cxx11::list<A*>’ to ‘std::__cxx11::list<Point*, std::allocator<Point*> >&’

If I were using lists of Point instead of lists of specialized elements, it would work, but I would be forced to cast the elements of these lists each time I need to use a method specific to the class (like update() for class A).

Is there any way that my list of A can be interpreted as a list of Point?

Thank for help!

Aucun commentaire:

Enregistrer un commentaire