怀疑std :: array C ++ 11库坏了吗?我该怎么办?

时间:2014-08-11 02:09:40

标签: c++ arrays c++11

从谷歌我似乎无法找到明确的答案,但我不知道该怎么做。我怀疑在我用g ++的数组C ++ 11库编写的一个更大的程序中可能存在问题,并编写了一个测试程序。我正在使用g ++ 4.8.1。

main.cpp中:

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

    return 0;
}

我的测试命令行编译是:

g++ main.cpp -std=c++11

有错误..

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 ++版本。

3 个答案:

答案 0 :(得分:8)

事实证明,一个拼写错误进入了OP的<array>。应该是什么

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

成了

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

在尝试处理<array>的其余部分时导致数百万编译错误。

答案 1 :(得分:0)

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

您得到的错误很可能是由于方言选择:C ++ 98模式不提供std::array容器。请参阅http://en.cppreference.com/w/cpp/container/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添加到命令行,就可以了。

答案 2 :(得分:0)

无法使用g++ 4.8.1 at Ideone重现:

enter image description here

错误消息与 not 提供选项-std=c++11

一致

将该选项添加到命令行