Priority Queue comparator problems

Sep 15, 2012 at 12:32am
This is basically what I have in main:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
struct CompareBoard : public std::binary_function<Board*, Board*, bool>                                                                                     
{
  bool operator()(const Board* lhs, const Board* rhs) const
  {
    return (lhs->getCost()+lhs->getDistance()) > (rhs->getCost()+rhs->getDistance());
  }
};

int main()
{
    vector<Board*> * vec = new Vector<Board*>*;
    std::priority_queue<Board*, std::vector<Board*>, CompareBoard> * pq = new std::priority_queue<Board*, std::vector<Board*>, CompareBoard>;

    board->generateChildren(pq, vec);
}


The board class is as follows:
1
2
3
4
5
6
7
8
9
10
11
12
struct CompareBoard;

class Board
{
    public:
       ...
            bool generateChildren(std::priority_queue<Board*, std::vector<Board*>, CompareBoard>* pq, std::vector<Board*>* vec );
       ...

    private:
        ...
};


I was having problems with CompareBoard in the board class so I tried a forward declaration, which resolved the problem until I add code that does anything with the PQ. On compile I get the following errors:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
In file included from c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/queue:65
:0,
                 from Board.h:4,
                 from board.cpp:2:
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_queue.h: In instantia
tion of 'std::priority_queue<Board*, std::vector<Board*>, CompareBoard>':
board.cpp:126:14:   instantiated from here
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_queue.h:380:18: error
: 'std::priority_queue<_Tp, _Sequence, _Compare>::comp' has incomplete type
Board.h:6:8: error: forward declaration of 'struct CompareBoard'
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_queue.h: In member fu
nction 'void std::priority_queue<_Tp, _Sequence, _Compare>::push(const value_typ
e&) [with _Tp = Board*, _Sequence = std::vector<Board*>, _Compare = CompareBoard,
 std::priority_queue<_Tp, _Sequence, _Compare>::value_type = Board*]':
board.cpp:126:29:   instantiated from here
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_queue.h:491:2: error:
 using invalid field 'std::priority_queue<_Tp, _Sequence, _Compare>::comp'


I assume it's a problem with the comparator being declared outside the class and I was wondering if someone could tell me how to go about making the PQ comparator available within main and the class. Or if it's some other problem, what that problem is.
Last edited on Sep 15, 2012 at 12:34am
Sep 15, 2012 at 1:33am
I figured out a way around my problem. Instead of calling the Board class members inside the compare struct, i changed it to call an overloaded comparison operator.

1
2
3
4
5
6
7
struct CompareBoard : public std::binary_function<Board*, Board*, bool>                                                                                     
{
  bool operator()(const Board* lhs, const Board* rhs) const
  {
    return (*lhs < *rhs);
  }
};
Topic archived. No new replies allowed.