2

我已经设法解决了(似乎是)大多数最初的问题,但由于我的一些答案来自其他人的(未回答的)问题,我不确定我是否朝着正确的方向前进。我正在使用 boost 的可变队列,并且在编译时遇到了一些问题。

我的代码,就目前而言,是:

typedef struct _MyComparator 
{
    bool operator() (const PriorityStruct* arg1, const PriorityStruct* arg2) const
    {
        return arg1->key < arg2->key; 
    }
} MyComparator;

class PriorityQueue
{
    typedef PriorityStruct* entry;
    typedef boost::typed_identity_property_map<entry> prop_map; 
    typedef boost::mutable_queue<entry, std::vector<entry>, MyComparator, prop_map> priority_queue_notAmb;
    priority_queue_notAmb* pq;
    boost::mutex mut;
public:
    PriorityQueue(void);
}

PriorityQueue::PriorityQueue()
{
    pq = new priority_queue_notAmb(sizeof(entry)*2, MyComparator(), prop_map());
    PriorityStruct* ps = new PriorityStruct;
    pq->push(ps);
}

当我删除最后一行 pq->push(ps) 时,代码将在创建新的 PriorityQueue 类型时编译并运行。事实上,我得到了阻止它编译的错误。

我的错误是:

1>------ Build started: Project: PriorityQueue, Configuration: Debug x64 ------
1>  PriorityQueue.cpp
1>C:\boost\boost\pending\mutable_queue.hpp(94): error C2679: binary '[' : no operator found which takes a right-hand operand of type 'PriorityQueue::entry ' (or there is no acceptable conversion)
1>          C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\vector(1119): could be 'const unsigned __int64 &std::vector<_Ty>::operator [](unsigned __int64) const'
1>          with
1>          [
1>              _Ty=size_t
1>          ]
1>          C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\vector(1135): or       'unsigned __int64 &std::vector<_Ty>::operator [](unsigned __int64)'
1>          with
1>          [
1>              _Ty=size_t
1>          ]
1>          while trying to match the argument list '(std::vector<_Ty>, PriorityQueue::entry )'
1>          with
1>          [
1>              _Ty=size_t
1>          ]
1>          C:\boost\boost\pending\mutable_queue.hpp(91) : while compiling class template member function 'void boost::mutable_queue<IndexedType,RandomAccessContainer,Comp,ID>::push(const IndexedType &)'
1>          with
1>          [
1>              IndexedType=PriorityQueue::entry,
1>              RandomAccessContainer=std::vector<PriorityQueue::entry>,
1>              Comp=MyComparator,
1>              ID=PriorityQueue::prop_map
1>          ]
1>          PriorityQueue.cpp(7) : see reference to function template instantiation 'void boost::mutable_queue<IndexedType,RandomAccessContainer,Comp,ID>::push(const IndexedType &)' being compiled
1>          with
1>          [
1>              IndexedType=PriorityQueue::entry,
1>              RandomAccessContainer=std::vector<PriorityQueue::entry>,
1>              Comp=MyComparator,
1>              ID=PriorityQueue::prop_map
1>          ]
1>          PriorityQueue.cpp(5) : see reference to class template instantiation 'boost::mutable_queue<IndexedType,RandomAccessContainer,Comp,ID>' being compiled
1>          with
1>          [
1>              IndexedType=PriorityQueue::entry,
1>              RandomAccessContainer=std::vector<PriorityQueue::entry>,
1>              Comp=MyComparator,
1>              ID=PriorityQueue::prop_map
1>          ]
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

任何帮助将不胜感激,因为我无法找到有关如何使用它的太多信息。

编辑:作为附加测试,我添加了

cout << (pq->empty() ? "YES" : "NO") << endl;

看看有没有什么奇怪的事情发生。它每次都打印“YES”。我用它替换了 pq->push,但除非我收到一两条评论要求我这样做以提高可读性,否则我会将其保留为更新的注释。

4

0 回答 0