zoukankan      html  css  js  c++  java
  • error C2872: “array”: 不明确的符号 // C++11的std::array初始化问题

    #include <iostream>
    #include <string>
    using namespace std;
    template<class T1,class T2>
    void MyForeach(T1 begin,T1 end,T2 op){
        for(T1 i=begin;i<end;i++){
            op(*i);
        }
    }
    void Print(string s)
    {
        cout << s;
    }
    void Inc(int & n)
    {
        ++ n;
    }
    string array[100];
    int a[100];
    int main() {
        int m,n;
        while(cin >> m >> n) {
            for(int i = 0;i < m; ++i)
                cin >> array[i];
            for(int j = 0; j < n; ++j)
                cin >> a[j];
            MyForeach(array,array+m,Print);         
            cout << endl;
            MyForeach(a,a+n,Inc);         
            for(int i = 0;i < n; ++i)
                cout << a[i] << ",";
            cout << endl;
        }
        return 0;
    }

    std::array中的元素必须在编译期间就要初始化,否则会出现一下错误:

    error C2280: 'std::array<>::array(void)': attempting to reference a deleted function

    std::array正确的使用方法如下:

    Cpp代码  收藏代码
    1. std::array<int, 3> a1{ {1, 2, 3} };  

    如果元素是动态添加的,使用std::vector。

    std命名空间里面已经定义了array了,你需要换个名字,或者在定义自己的这个array之前,不要使用using namespace std;

    你用了C++保留字或是某个命名空间的关键字(std::array),将array重命名为另外一个名称可以解决这个问题。

    编译信息已经告诉你了呀,你的 array 和标准库里的 std::array 命名冲突了,std::array是C++11里新引入的,另外不要轻易使用using namespace std;

  • 相关阅读:
    如何使用Log4j
    HDU 1114
    老鼠与毒药问题
    HDU 1065
    HDU 1301(MST)
    HDU 1078
    HDU 2159
    删除字符问题(贪心)
    正整数分解为几个连续自然数之和
    OpenJudge
  • 原文地址:https://www.cnblogs.com/focus-z/p/11173332.html
Copyright © 2011-2022 走看看