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;
andList x();
?A big difference!
Suppose that
List
is the name of some class. Then functionf()
declares a localList
object calledx
:void f() { List x; // Local object named x (of class List) ... }
But function
g()
declares a function calledx()
that returns aList
:void g() { List x(); // Function named x (that returns a List) ... }
Replace queue<int> myqueue();
by queue<int> myqueue;
and you'll be fine.
精彩评论