Queue from stl

I am trying to get the following code to compile with g ++ 4.2.1 and getting 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 typestd::queue<int, std::deque<int, std::allocator<int> > > ()()’
main.cpp:12: error: request for member ‘size’ in ‘myqueue’, which is of non-class typestd::queue<int, std::deque<int, std::allocator<int> > > ()()’

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

+3
source share
1 answer

C ++ FAQ Lite § 10.2

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

A big difference!

Suppose that Listis the name of a class. The function then f()declares a local object Listwith the name x:

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

But the function g()declares a function with a name x()that returns a List:

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

Replace queue<int> myqueue();with queue<int> myqueue;, and everything will be fine.

+10
source

Source: https://habr.com/ru/post/1729012/


All Articles