开发者

queue from the stl

I am trying to get the following code to compile using g++ 4.2.1 and am receiving the following errors

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);

 开发者_Python百科   cout << myqueue.size();

    return 0;
}

ERRORS:

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> > > ()()’

Any ideas as to why? I tried in Eclipse, X-Code and through the terminal.


C++ FAQ Lite § 10.2

Is there any difference between List x; and List x();?

A big difference!

Suppose that List is the name of some class. Then function f() declares a local List object called x:

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

But function g() declares a function called x() that returns a List:

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

Replace queue<int> myqueue(); by queue<int> myqueue; and you'll be fine.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜