来自stl的队列

fin*_*oop 3 c++ stl g++ standard-library

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

码:

#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;
}
Run Code Online (Sandbox Code Playgroud)

错误:

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> > > ()()’
Run Code Online (Sandbox Code Playgroud)

任何想法为什么?我在Eclipse,X-Code和终端中尝试过.

eph*_*ent 10

C++ FAQLite§10.2

有什么区别List x;List x();

一个很大的区别!

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

void f()
{
  List x;     // Local object named x (of class List)
  ...
}
Run Code Online (Sandbox Code Playgroud)

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

void g()
{
  List x();   // Function named x (that returns a List)
  ...
}
Run Code Online (Sandbox Code Playgroud)

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