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;

 

转载于:https://www.cnblogs.com/focus-z/p/11173332.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值