我正在尝试使用g ++ 4.2.1编译以下代码并收到以下错误
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;
}
错误:
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> > > ()()’
为什么有任何想法?我尝试使用Eclipse,X-Code和终端。
答案 0 :(得分:10)
List x;
和List x();
之间是否存在差异?大的差异!
假设
List
是某个类的名称。然后函数f()
声明一个名为List
的本地x
对象:void f() { List x; // Local object named x (of class List) ... }
但是函数
g()
声明了一个名为x()
的函数,它返回List
:void g() { List x(); // Function named x (that returns a List) ... }
将queue<int> myqueue();
替换为queue<int> myqueue;
,你会没事的。