finiteloop
finiteloop

Reputation: 4496

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

    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.

Upvotes: 3

Views: 1021

Answers (1)

ephemient
ephemient

Reputation: 204708

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.

Upvotes: 10

Related Questions