std :: sort的错误

时间:2014-07-05 09:25:51

标签: c++ sorting std

我创建了Node类的一些实例和Node类的向量,然后我将这些实例推送到vector中, 我创建了函数对象" ListCompare"对矢量进行排序。

但是,我收到错误"没有匹配函数来调用#34; ListCompare"类型的对象。 "在排序功能。

为什么我收到错误?

我在下面写了代码和错误。

#include <iostream>
#include "cocos2d.h"

using namespace cocos2d;

class Node
{

public:
    Node(int x,int y,CCPoint playerTilePos): m_tilePosX(x),m_tilePosY(y),m_costFromStart(0),m_costFromNextToGoal(0),m_playerTilePos(playerTilePos){};
    Node(const Node &obj);
    virtual ~Node(){};

    int getPosX(void) const { return m_tilePosX; }
    int getPosY(void) const { return m_tilePosY; }

    int getTotalCost(void) const { return m_costFromStart + m_costFromNextToGoal; }

    int getConstFromStart(void) const { return m_costFromStart; }
    void setCostFromStart(int var) { m_costFromStart = var; }

    int getCostFromNextToGoal(void)const { return ( std::abs((int)m_playerTilePos.x - m_tilePosX) + std::abs((int)m_playerTilePos.y - m_tilePosY) );}
    void setCostNextToGoal(int var) { m_costFromNextToGoal = var; }


    bool operator == (Node node)
    {
        return (m_tilePosX == node.m_tilePosX && m_tilePosY == node.m_tilePosY);
    }



    void operator = (Node node)
    {
        m_tilePosX = node.m_tilePosX;
        m_tilePosY = node.m_tilePosY;
        m_costFromStart = node.m_costFromStart;
        m_costFromNextToGoal = node.m_costFromNextToGoal;
    }





private:

    int m_tilePosX;
    int m_tilePosY;     
    int m_costFromStart; 
    int m_costFromNextToGoal; 
    CCPoint m_playerTilePos;
};


std::vector<Node>List;



class ListCompare{
public:
   bool operator()(Node& pNode1,Node& pNode2)
   {
            return pNode1.getTotalCost() > pNode2.getTotalCost();
   }
};








//--------------------------------------------------------------
//             START 
//--------------------------------------------------------------

void main()
{

  List openList;

  //initialize
  CCPoint pos = ccp(100,100);

  Node startNode(10,10,pos);

  //cost is 1000
  startNode.setCostNextToGoal(1000);

  std::cout << startNode.getTotalCost << std::endl; //totalcost = 0 + 1000 = 1000

  openList.pushBack(startNode);  



  Node nextNode(20,20,pos);

  NextNode.setCostNextToGoal(2000);

  std::cout << NextNode.getTotalCost << std::endl; //totalcost = 0 + 2000 = 2000


  openList.pushBack(NextNode);


  std::sort(openList.begin(),openList.end(),ListCompare());

 }

--------------------------------错误------------- ---------------------

template<typename _Tp, typename _Compare>
    inline const _Tp&
    __median(const _Tp& __a, const _Tp& __b, const _Tp& __c, _Compare __comp)
    {
      // concept requirements
      __glibcxx_function_requires(_BinaryFunctionConcept<_Compare,bool,_Tp,_Tp>)

      if (__comp(__a, __b)) //the error part."No matching function for call to object of type "ListCompare" "
    if (__comp(__b, __c))
      return __b;
    else if (__comp(__a, __c))
      return __c;
    else
      return __a;
    else if (__comp(__a, __c))
      return __a;
    else if (__comp(__b, __c))
      return __c;
    else
      return __b;
}

1 个答案:

答案 0 :(得分:2)

ListCompare::operator()中,您需要将参数作为const引用。

class ListCompare
{
public:
    bool operator()(const Node& pNode1, const Node& pNode2) const
    {
        return pNode1.getTotalCost() > pNode2.getTotalCost();
    }
};