来自stl的队列

时间:2010-01-19 03:29:44

标签: c++ stl g++ standard-library

我正在尝试使用g ++ 4.2.1编译以下代码并收到以下错误

CODE:

#include <iostream>
#include <queue>

using namespace std;

int main (int argc, char * const argv[])
{    
    queue<int> myqueue();
    for(int i = 0; i < 10; i++)
        myqueue.push(i);

    cout << myqueue.size();

    return 0;
}

错误:

main.cpp: In function ‘int main(int, char* const*)’:
main.cpp:10: error: request for member ‘push’ in ‘myqueue’, which is of non-class type ‘std::queue<int, std::deque<int, std::allocator<int> > > ()()’
main.cpp:12: error: request for member ‘size’ in ‘myqueue’, which is of non-class type ‘std::queue<int, std::deque<int, std::allocator<int> > > ()()’

为什么有任何想法?我尝试使用Eclipse,X-Code和终端。

1 个答案:

答案 0 :(得分:10)

C++ FAQ Lite § 10.2

  

List x;List x();之间是否存在差异?

     

的差异!

     

假设List是某个类的名称。然后函数f()声明一个名为List的本地x对象:

void f()
{
  List x;     // Local object named x (of class List)
  ...
}
     

但是函数g()声明了一个名为x()的函数,它返回List

void g()
{
  List x();   // Function named x (that returns a List)
  ...
}

queue<int> myqueue();替换为queue<int> myqueue;,你会没事的。