我在使用C++的時候,遇到了如下報錯:
-> % g++ test.cpp
In file included from /usr/include/c++/7/queue:64:0,
from test.cpp:5:
/usr/include/c++/7/bits/stl_queue.h: In instantiation of ‘class std::queue<int, int>’:
test.cpp:33:21: required from here
/usr/include/c++/7/bits/stl_queue.h:124:47: error: ‘int’ is not a class, struct, or union type
typedef typename _Sequence::value_type value_type;
^~~~~~~~~~
/usr/include/c++/7/bits/stl_queue.h:125:46: error: ‘int’ is not a class, struct, or union type
typedef typename _Sequence::reference reference;
^~~~~~~~~
/usr/include/c++/7/bits/stl_queue.h:126:51: error: ‘int’ is not a class, struct, or union type
typedef typename _Sequence::const_reference const_reference;
^~~~~~~~~~~~~~~
/usr/include/c++/7/bits/stl_queue.h:127:46: error: ‘int’ is not a class, struct, or union type
typedef typename _Sequence::size_type size_type;
^~~~~~~~~
使用clang編譯的話報錯更不容易懂:
-> % clang++ test.cpp
In file included from test.cpp:5:
In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/7.4.0/../../../../include/c++/7.4.0/queue:64:
/usr/bin/../lib/gcc/x86_64-linux-gnu/7.4.0/../../../../include/c++/7.4.0/bits/stl_queue.h:124:24: error: type 'int' cannot be used
prior to '::' because it has no members
typedef typename _Sequence::value_type value_type;
^
test.cpp:33:21: note: in instantiation of template class 'std::queue<int, int>' requested here
queue<int, int> q;
^
In file included from test.cpp:5:
In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/7.4.0/../../../../include/c++/7.4.0/queue:64:
/usr/bin/../lib/gcc/x86_64-linux-gnu/7.4.0/../../../../include/c++/7.4.0/bits/stl_queue.h:125:24: error: type 'int' cannot be used
prior to '::' because it has no members
typedef typename _Sequence::reference reference;
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/7.4.0/../../../../include/c++/7.4.0/bits/stl_queue.h:126:24: error: type 'int' cannot be used
prior to '::' because it has no members
typedef typename _Sequence::const_reference const_reference;
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/7.4.0/../../../../include/c++/7.4.0/bits/stl_queue.h:127:24: error: type 'int' cannot be used
prior to '::' because it has no members
typedef typename _Sequence::size_type size_type;
^
4 errors generated.
報錯是因爲我錯誤地聲明瞭:
queue<int, int> q;
正確的聲明方法如下:
queue<int> q;
queue <pair<int, int> > q2;