2014-07-05 2 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()); 

} 

-------------------------------- 오류 --------- ------------------------- ListCompare::operator()에서

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' 멤버를 복사하지 않으면 std :: sort가 const 참조를 매개 변수로 사용하도록 변경하더라도 올바르게 작동하지 않습니다. – PaulMcKenzie

+0

답변 해 주셔서 감사합니다. 회신이 늦어서 죄송합니다. 내 코드에서 복사 생성자의 정의를 쓰는 것을 잊었다. 내가 const 참조를 사용하도록 변경했을 때, 나는 오류를 해결했다. – user3321541

답변

2

당신은 const를 참조로 매개 변수를 취할 필요가있다.

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

답변 해 주셔서 감사합니다. "const"라고 쓰지 않았 음을 알지 못했습니다. 문제가 생겨서 죄송합니다. – user3321541

관련 문제