命名空间“std”中没有名为“array”的成员

no member named 'array' in namespace 'std'

提问人: 提问时间:12/5/2018 更新时间:12/5/2018 访问量:2191

问:

我现在正在学习 C++,我得到了这个有点奇怪的错误。

守则如下:

#include <iostream>
#include <array>
using std::cout;
using std::endl;
using std::ostream;
using std::array;

template <typename T, size_t dim>
ostream& operator<<(ostream& os, const array<T,dim>& a) {
    os << "[ ";
    for (auto n : a)
        os << n << " ";
    os << "]";
    return os;
}

int main() 
{
    cout << endl << "--- " << __FILE__ << " ---" << endl << endl;

    array<int,3> a1 { 2,3,5 };                              // (A)
    array<int,0> a2 {  };                                   // (B)
    array<int,2> a3 { 1 };                                  // (C)
    // array<int> x1 { 1, 2, 3 };                           // (D)
    // array<int,3> x2 { 1,2,3,4 };
    array<int,3> a4 = { 1,2,3 };                            // (E)
    array<int,3> a5 { { 4,5,6 } };                          // (F)

    cout << "01|    a1=" << a1 << endl;
    cout << "02|    a2=" << a2 << endl;
    cout << "03|    a3=" << a3 << endl;
    cout << "04|    a4=" << a4 << endl;
    cout << "05|    a5=" << a5 << endl;

    cout << endl << "--- " << __FILE__ << " ---" << endl << endl;
    return 0;
}

我的 IDE (Visual Studio Code) 向我显示错误,尽管代码正在编译和工作。

这是我们的教授提供的 makefile。

# compiler settings
CXX = g++-7
# CXX = clang++
CXXFLAGS = -ansi -pedantic -Wall -Wextra -Wconversion -pthread -std=c++17
LDFLAGS = -lm

# collect  files
CXXEXAMPLES = $(shell find . -name '*.cpp' -print -type f)
CXXTARGETS = $(foreach file, $(CXXEXAMPLES), ./out/$(file:.cpp=.out))

# build them all
all: $(CXXTARGETS)

out/%.out: %.cpp
    $(CXX) $(CXXFLAGS)  $< $(LDFLAGS) -o $@

clean:
    rm out/*

我使用 Ubuntu 16.04 并认为这可能是编译器问题,所以我将“CXX”更改为“CXX = g++-7”,因为建议我们使用 g++ 版本 7,但它没有帮助。 在输入“g++ -v”时,它显示我的 gcc 是 5.5.0 版本,但输入“apt list -installed”显示已安装 g++-7。

我在互联网上没有找到任何解决方案,因为大多数类似的问题通常都围绕着缺少包含。

VS Code 也无法识别某些类型的变量定义,例如 “int n{1}” 它还对(A)至(E)行中的“使用未申报的标识符”提出申诉

我认为问题出在使用不同/旧语法识别的 VS Code 编译器中。但我不知道如何改变这一点。

C++ visual-studio-code 语法错误 ubuntu-16.04

评论

0赞 Matthieu Brucher 12/5/2018
您是否配置了 VSC 来理解 C++17?
1赞 Jesper Juhl 12/5/2018
传递给编译器以启用 C++17。并阅读手册“man g++”或 gcc.gnu.org/onlinedocs-std=c++17
0赞 zett42 12/5/2018
@JesperJuhl 它已经在 makefile 中。
0赞 Damien 12/5/2018
如果它编译,它只是一个警告......
0赞 12/5/2018
@MatthieuBrucher我刚刚用谷歌搜索了一下,发现了一个 c_cpp_properties.json 文件。我想这可能很重要 “compilerPath”: “/usr/bin/clang”, “cStandard”: “c11”, “cppStandard”: “c++17”, “intelliSenseMode”: “clang-x64” 在哪里可以配置 VSC 以了解 C++17?

答: 暂无答案