Rog*_*_88 0 c++ arrays initialization constants c++11
当我尝试使用const int std :: array时为什么会出现以下错误?我必须在错误消息中将"<"替换为"<\"以正确显示它:
#include <array>
#include <iostream>
using namespace std;
int main() {
array<const int, 4> vals;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
test.cpp:在函数'int main()'中:
test.cpp:7:22:错误:使用已删除的函数'std :: array :: array()'
array <\ const int,4> vals; ^ ~~~
在test.cpp中包含的文件中:1:0:/usr/include/c++/7.3.0/array:94:12:注意:'std :: array <\ const int,4> :: array()'是隐式删除,因为默认定义不正确:struct array ^ ~~~~
/usr/include/c++/7.3.0/array:94:12:错误:'struct std :: array <\ const int,4>'中未初始化的const成员
/usr/include/c++/7.3.0/array:110:56:注意:'const int std :: array <\ const int,4> :: _ M_elems [4]'应初始化typename _AT_Type :: _ Type _M_elems; ^ ~~~~~~~
你打算如何为const int元素分配数组值?您应该在声明期间初始化它们.
#include <array>
using namespace std;
int main() {
array<const int, 4> vals{1, 2, 3, 4};
return 0;
}
Run Code Online (Sandbox Code Playgroud)
没有任何初始化,您的示例类似于无效的const声明:
const int a;
Run Code Online (Sandbox Code Playgroud)
由于std::array本身无法更新,我认为以下代码可以更清楚地理解.
#include <array>
using namespace std;
int main() {
const array<int, 4> vals{1, 2, 3, 4};
return 0;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
402 次 |
| 最近记录: |