正确使用c ++标准队列

时间:2014-05-23 19:16:00

标签: c++ visual-studio queue

我正在使用Visual Studio 2013。 我使用以下代码作为我的代码的一部分:

#include <queue>
#include <curses> // pdcurses for mvprintw function
using namespace std;
typedef unsigned short ushort;    
struct xy{
    int x;
    int y;
};    
void move(ushort length, queue<xy>& test);    
int main() {
  // ...
}    
void move(ushort length, queue<xy>& test) {
    queue<xy> coord;    
    if (length <= test.size()) {
        coord = test.pop();
        mvprintw(coord.y, coord.x, "  ");
    }    
    // ...
}

如果我要使用我制作的队列(不允许使用模板),将其设置为使用该结构作为其类型,它可以正常工作。但是,我想使用模板化队列,所以我也可以使用其他类型的队列。但是当我以上面给出的方式使用c ++标准队列时,我收到以下错误:

error C2679: binary '=' : no operator found which takes a right-hand operand of type 'void' (or there is no acceptable conversion)
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\queue(101): could be 'std::queue<xy,std::deque<_Ty,std::allocator<_Ty>>> &std::queue<_Ty,std::deque<_Ty,std::allocator<_Ty>>>::operator =(std::queue<_Ty,std::deque<_Ty,std::allocator<_Ty>>> &&)'
1>          with
1>          [
1>              _Ty=xy
1>          ]
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\queue(43): or       'std::queue<xy,std::deque<_Ty,std::allocator<_Ty>>> &std::queue<_Ty,std::deque<_Ty,std::allocator<_Ty>>>::operator =(const std::queue<_Ty,std::deque<_Ty,std::allocator<_Ty>>> &)'
1>          with
1>          [
1>              _Ty=xy
1>          ]
1>          while trying to match the argument list '(std::queue<xy,std::deque<_Ty,std::allocator<_Ty>>>, void)'
1>          with
1>          [
1>              _Ty=xy
1>          ]

我错过了一些简单的东西吗?我不明白为什么它似乎认为pop函数返回一个void类型。队列不使用pop()来实现我的想法吗?或者我在代码中使用队列的错误是什么?

1 个答案:

答案 0 :(得分:2)

您似乎要将coord声明为xy类型,而不是queue<xy>

std::queue弹出确实没有返回;它只是删除了前面的元素。如果你想要前面的元素,请拨打front然后弹出。

coord = test.front();
test.pop();