2014-07-05 87 views
-2

的错误我创建了Node类的一些实例和Node类的向量,然后我将这些实例推入向量, ,我创建了函数对象“ListCompare”对向量进行排序。std :: sort

但是,我收到错误“没有匹配函数调用类型为”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

请张贴一些最小的代码来重现问题。但是,确保'ListCompare'是在std :: sort'中使用之前定义的,并将相关签名更改为'bool operator()(const Node&pNode1,const Node&pNode2)const' – juanchopanza

+0

您的赋值和拷贝构造函数在如果使用'Node'类将显示未定义的行为。您无法复制'CCPoint'成员,因此,即使您更改为使用const引用作为参数,std :: sort也无法正常工作。 – PaulMcKenzie

+0

谢谢你的回答。很抱歉,答复迟到了。我忘了在我的代码中定义复制构造函数。当我更改为使用const引用时,我解决了错误。 – user3321541

回答

2

ListCompare::operator()您需要将参数作为常量引用。

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

谢谢你的回答。我没有注意到我没有写“const”,我很抱歉给你造成麻烦。 – user3321541

相关问题