可疑std::array C++11库已损坏?我该怎么办?

可疑std::array C++11库已损坏?我该怎么办?,c++,arrays,c++11,C++,Arrays,C++11,从谷歌我似乎找不到明确的答案,但我不知道该怎么办。我怀疑我用array C++11库为g++编写的一个更大的程序可能有问题,并编写了一个测试程序。我使用的是g++4.8.1 main.cpp: #include <iostream> #include <array> int main() { std::array<int,5> vector; return 0; } 有错误 In file included from main.cpp:2:

从谷歌我似乎找不到明确的答案,但我不知道该怎么办。我怀疑我用array C++11库为g++编写的一个更大的程序可能有问题,并编写了一个测试程序。我使用的是g++4.8.1

main.cpp:

#include <iostream>
#include <array>
int main()
{
    std::array<int,5> vector;

    return 0;
}
有错误

In file included from main.cpp:2:0:
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\array:107:12: error: 'array' has not been declared
       swap(array& __other)
            ^
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\array: In member function 'void std::Array<_Tp, _Nm>::swap(int&)':
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\array:109:50: error: request for member 'begin' in '__other', which is of non-class type 'int'
       { std::swap_ranges(begin(), end(), __other.begin()); }
                                                  ^
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\array: At global scope:
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\array:228:22: error: 'array' does not name a type
     operator==(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
                      ^
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\array:228:27: error: expected ',' or '...' before '<' token
     operator==(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
                           ^
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\array:228:74: error: 'bool std::operator==(int)' must have an argument of class or enumerated type
     operator==(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
                                                                          ^
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\array:233:22: error: 'array' does not name a type
     operator!=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
                      ^
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\array:233:27: error: expected ',' or '...' before '<' token
     operator!=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
                           ^
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\array:233:74: error: 'bool std::operator!=(int)' must have an argument of class or enumerated type
     operator!=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
                                                                          ^
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\array:238:21: error: 'array' does not name a type
     operator<(const array<_Tp, _Nm>& __a, const array<_Tp, _Nm>& __b)
                     ^
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\array:238:26: error: expected ',' or '...' before '<' token
     operator<(const array<_Tp, _Nm>& __a, const array<_Tp, _Nm>& __b)
                          ^
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\array:238:69: error: 'bool std::operator<(int)' must have an argument of class or enumerated type
     operator<(const array<_Tp, _Nm>& __a, const array<_Tp, _Nm>& __b)
                                                                     ^
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\array:246:21: error: 'array' does not name a type
     operator>(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
                     ^
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\array:246:26: error: expected ',' or '...' before '<' token
     operator>(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
                          ^
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\array:246:73: error: 'bool std::operator>(int)' must have an argument of class or enumerated type
     operator>(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
                                                                         ^
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\array:251:22: error: 'array' does not name a type
     operator<=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
                      ^
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\array:251:27: error: expected ',' or '...' before '<' token
     operator<=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
                           ^
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\array:251:74: error: 'bool std::operator<=(int)' must have an argument of class or enumerated type
     operator<=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
                                                                          ^
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\array:256:22: error: 'array' does not name a type
     operator>=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
                      ^
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\array:256:27: error: expected ',' or '...' before '<' token
     operator>=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
                           ^
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\array:256:74: error: 'bool std::operator>=(int)' must have an argument of class or enumerated type
     operator>=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
                                                                          ^
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\array:262:10: error: variable or field 'swap' declared void
     swap(array<_Tp, _Nm>& __one, array<_Tp, _Nm>& __two)
          ^
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\array:262:10: error: 'array' was not declared in this scope
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\array:262:19: error: expected primary-expression before ',' token
     swap(array<_Tp, _Nm>& __one, array<_Tp, _Nm>& __two)
                   ^
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\array:262:27: error: '__one' was not declared in this scope
     swap(array<_Tp, _Nm>& __one, array<_Tp, _Nm>& __two)
                           ^
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\array:262:34: error: 'array' was not declared in this scope
     swap(array<_Tp, _Nm>& __one, array<_Tp, _Nm>& __two)
                                  ^
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\array:262:43: error: expected primary-expression before ',' token
     swap(array<_Tp, _Nm>& __one, array<_Tp, _Nm>& __two)
                                           ^
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\array:262:51: error: '__two' was not declared in this scope
     swap(array<_Tp, _Nm>& __one, array<_Tp, _Nm>& __two)
                                                   ^
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\array:268:9: error: template declaration of 'constexpr _Tp& std::get'
     get(array<_Tp, _Nm>& __arr) noexcept
         ^
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\array:268:9: error: 'array' was not declared in this scope
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\array:268:18: error: expected primary-expression before ',' token
     get(array<_Tp, _Nm>& __arr) noexcept
                  ^
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\array:268:26: error: '__arr' was not declared in this scope
     get(array<_Tp, _Nm>& __arr) noexcept
                          ^
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\array:277:9: error: template declaration of 'constexpr _Tp&& std::get'
     get(array<_Tp, _Nm>&& __arr) noexcept
         ^
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\array:277:9: error: 'array' was not declared in this scope
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\array:277:18: error: expected primary-expression before ',' token
     get(array<_Tp, _Nm>&& __arr) noexcept
                  ^
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\array:277:27: error: label '__arr' referenced outside of any function
     get(array<_Tp, _Nm>&& __arr) noexcept
                           ^
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\array:277:27: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\array:285:15: error: 'array' does not name a type
     get(const array<_Tp, _Nm>& __arr) noexcept
               ^
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\array:285:20: error: expected ',' or '...' before '<' token
     get(const array<_Tp, _Nm>& __arr) noexcept
                    ^
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\array: In function 'constexpr const _Tp& std::get(int)':
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\array:289:9: error: '__arr' was not declared in this scope
  _S_ref(__arr._M_elems, _Int);
         ^
In file included from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\iostream:38:0,
                 from main.cpp:1:
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\array: At global scope:
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\array:306:23: error: 'array' is not a member of 'std'
     struct tuple_size<_GLIBCXX_STD_C::array<_Tp, _Nm>>
                       ^
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\array:306:23: error: 'array' is not a member of 'std'
In file included from main.cpp:2:0:
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\array:306:50: error: wrong number of template arguments (2, should be 1)
     struct tuple_size<_GLIBCXX_STD_C::array<_Tp, _Nm>>
                                                  ^
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\array:303:11: error: provided for 'template<class _Tp> class std::tuple_size'
     class tuple_size;
           ^
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\array:306:53: error: expected unqualified-id before '>' token
     struct tuple_size<_GLIBCXX_STD_C::array<_Tp, _Nm>>
                                                     ^
In file included from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\iostream:38:0,
                 from main.cpp:1:
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\array:314:32: error: 'array' is not a member of 'std'
     struct tuple_element<_Int, _GLIBCXX_STD_C::array<_Tp, _Nm>>
                                ^
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\array:314:32: error: 'array' is not a member of 'std'
In file included from main.cpp:2:0:
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\array:314:59: error: wrong number of template arguments (3, should be 2)
     struct tuple_element<_Int, _GLIBCXX_STD_C::array<_Tp, _Nm>>
                                                           ^
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\array:311:11: error: provided for 'template<unsigned int _Int, class _Tp> class std::tuple_element'
     class tuple_element;
           ^
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\array:314:62: error: expected unqualified-id before '>' token
     struct tuple_element<_Int, _GLIBCXX_STD_C::array<_Tp, _Nm>>
                                                              ^
main.cpp: In function 'int main()':
main.cpp:5:2: error: 'array' is not a member of 'std'
  std::array<int,5> vector;
  ^
main.cpp:5:13: error: expected primary-expression before 'int'
  std::array<int,5> vector;
             ^
main.cpp:5:13: error: expected ';' before 'int'
显然,从错误中可以看出,我包含了很多对std::array文件的引用,这似乎是错误的根源。我该怎么办

提前谢谢


编辑:添加了编译命令和g++版本。

这是一个有效的C++11程序

您得到的错误很可能是由于方言选择:C++98模式不提供std::array容器。请参阅

具体而言,您的错误可归结为以下几点:

main.cpp: In function 'int main()':
main.cpp:5:2: error: 'array' is not a member of 'std'
  std::array<int,5> vector;
  ^
main.cpp:5:13: error: expected primary-expression before 'int'
  std::array<int,5> vector;
             ^
main.cpp:5:13: error: expected ';' before 'int'
因此,只需将-std=c++11添加到命令行中,事情就可以正常进行。

无法使用以下命令复制:

错误消息与未提供选项-std=c++11一致


将该选项添加到命令行

之后,不知何故,一个输入错误进入了OP的命令行。应该是什么

template<typename _Tp, std::size_t _Nm>
  struct array
  {
变成

template<typename _Tp, std::size_t _Nm>
  struct Array
  {

在处理其余的.g/C++时,会导致一个BAZLLION编译器错误。把你的变量命名为C++类是不明智的。@保尔麦肯齐:他命名变量向量是完全有效的。他没有使用using声明将std::vector引入范围。我不认为这是幸运的。是的,避免混淆标识符是很好的,但我认为这里的人们有点太挑剔了。@Cornstales:他没有看到这是无效的,他说这是不明智的,他是对的。void std::Array::swapint&’…嗯,这很奇怪。打开C:\MIW\LB\GC\\MIW32(4.C++),C++的C++数组,行到81行左右,是否是结构数组或结构数组?如果是问题,你会看到不同的错误:/Ur/Prime/C++/4.90/BIT/C++0xYAdvult.H:32:2:错误:错误:这个文件需要编译器和库支持ISO C++ 2011标准。此支持目前处于试验阶段,必须使用-std=c++11或-std=gnu++11编译器选项启用。

template<typename _Tp, std::size_t _Nm>
  struct Array
  {