Search code examples
qtsortingc++11path-finding

Sorting with a function , no result


I want to sort a list based on the shortest path to a destination.

I use the sort function, so i go from begin to the end and calculate which is the best path to go to the next enemy.

std::vector<std::shared_ptr<Enemy>> ek = model->getAllEnemies();
std::sort( ek.begin(), ek.end(), customLess);

changed it to a struct:

struct {
        bool operator()(const std::shared_ptr<Enemy> &e, const std::shared_ptr<Enemy> &nexthp)
        {
            int pathcost = findPathfindingNodes(e->getXPos(), e->getYPos(),true);
            int pathcost2 = findPathfindingNodes(nexthp->getXPos(), nexthp->getYPos(),true);
            return pathcost < pathcost2;
        }
    } customLess;

But when i want to use the findpathfindingnodes function which is declared in the same .cpp file it gives an error :

/controller.cpp:235: error: 'findPathfindingNodes' was not declared in this scope
             int pathcost = findPathfindingNodes(e->getXPos(), e->getYPos(),true);

Solution

  • just include the class that has that function in it...

    class SomeClass
    {
        public:
            SomeClass();
            int findPathfindingNodes();
    }
    

    then wherever you want to use it make sure you have included the class #include "SomeClass"

    then just create an instance of it and call the function

    SomeClass instance_of_some_class();
    int result = instance_of_some_class.findPathfindingNodes();