C++ 如何从方法返回静态常量int std::array?
我试图从一个函数返回一个常量int std::array,但是我遇到了错误,我不知道如何修复它们 不应更改C++ 如何从方法返回静态常量int std::array?,c++,arrays,c++11,C++,Arrays,C++11,我试图从一个函数返回一个常量int std::array,但是我遇到了错误,我不知道如何修复它们 不应更改refmatrix中的这些值。这就是为什么我使用常量int 参考文献.h #include <array> #include <iostream> using namespace std; class Reference { public: static const size_t NCOLS = 3; static const size_t NBI
refmatrix
中的这些值。这就是为什么我使用常量int
参考文献.h
#include <array>
#include <iostream>
using namespace std;
class Reference {
public:
static const size_t NCOLS = 3;
static const size_t NBITS = 4;
static const size_t NROWS = 4;
private:
static array<array<array<const int, NBITS>, NCOLS>, NROWS> refmatrix;
public:
Reference();
~Reference();
array<const int, NBITS> smaller(int *arry);
};
#包括
#包括
使用名称空间std;
课堂参考{
公众:
静态常数大小=3;
静态常数大小n比特=4;
静态常数大小=4;
私人:
静态阵列矩阵;
公众:
引用();
~Reference();
较小的数组(int*arry);
};
reference.cpp
#include "reference.h"
array<array<array<const int, Reference::NBITS>, Reference::NCOLS>, Reference::NROWS> Reference::refmatrix = {{{{{0, 1, 2, 6}, {6, 4, 1, 7}, {6, 7, 8, 3}}},
{{{7, 0, 5, 2}, {1, 6, 9, 3}, {1, 4, 8, 0}}},
{{{9, 3, 4, 6}, {0, 7, 2, 8}, {5, 3, 9, 4}}},
{{{8, 9, 1, 4}, {7, 2, 6, 0}, {4, 0, 3, 7}}}}};
Reference::Reference() {}
Reference::~Reference() {}
array<const int, Reference::NBITS> Reference::smaller(int *arry) {
int j;
for(int i=0; i < NROWS; i++){
for(j=0; j < NCOLS; j++){
if(refmatrix[i][0][j] != arry[j]){
j = (NCOLS + 1);
}
}
if(j == NCOLS){
return refmatrix[i][1];
}
}
}
#包括“reference.h”
数组引用::refmatrix={{{{0,1,2,6},{6,4,1,7},{6,7,8,3},
{{{7, 0, 5, 2}, {1, 6, 9, 3}, {1, 4, 8, 0}}},
{{{9, 3, 4, 6}, {0, 7, 2, 8}, {5, 3, 9, 4}}},
{{{8, 9, 1, 4}, {7, 2, 6, 0}, {4, 0, 3, 7}}}}};
Reference::Reference(){}
引用::~Reference(){}
数组引用::更小(int*arry){
int j;
对于(int i=0;i
main.cpp
#include "reference.h"
int main() {
Reference r;
int arr[3] = {0, 1, 2};
array<const int, Reference::NBITS> resp;
resp = r.smaller( arr );
// After get these values I will write it in a file.
return 0;
}
#包括“reference.h”
int main(){
参考r;
int-arr[3]={0,1,2};
阵列响应;
resp=较小的r(arr);
//得到这些值后,我会将其写入一个文件中。
返回0;
}
main.cpp:在函数“int main()”中:
main.cpp:6:37:错误:使用已删除的函数“std::array::array()”
阵列响应;
^~~~
在reference.h:1:0中包含的文件中,
来自main.cpp:1:
/usr/include/c++/7.3.0/array:94:12:注意:“std::array::array()”被隐式删除,因为默认定义的格式可能不正确:
结构体数组
^~~~~
/usr/include/c++/7.3.0/array:94:12:错误:“struct std::array”中未初始化的常量成员
/usr/include/c++/7.3.0/array:110:56:注意:“const int std::array::_M_elems[4]”应该初始化
typename _AT_Type::_Type _M_elems;
^~~~~~~~
main.cpp:9:33:错误:使用已删除的函数“std::array&std::array::operator=(std::array&)”
resp=较小的r(arr);
^
在reference.h:1:0中包含的文件中,
来自main.cpp:1:
/usr/include/c++/7.3.0/array:94:12:注意:“std::array&std::array::operator=(std::array&&)”被隐式删除,因为默认定义的格式可能不正确:
结构体数组
^~~~~
/usr/include/c++/7.3.0/array:94:12:错误:非静态常量成员“const int std::array::M_elems[4]”,无法使用默认赋值运算符
该错误与返回
std::array
无关。这与分配给常量有关
您需要使用调用初始化respresp
,而不是默认初始化然后分配
int main() {
Reference r;
int arr[3] = {0, 1, 2};
array<const int, Reference::NBITS> resp = r.smaller( arr );
// After get these values I will write it in a file.
return 0;
}
intmain(){
参考r;
int-arr[3]={0,1,2};
阵列响应=较小的r(arr);
//得到这些值后,我会将其写入一个文件中。
返回0;
}
阅读错误消息并查看它指向的代码。问题与返回数组无关。以后不能将const int
赋值给std::array
元素,就像不能const int i;i=123代码>如果我在main.cpp中更改为array resp;在2个不同的部分中标记一个类的公共成员是灾难应用程序的秘诀,“标记”和“2个不同的部分”是什么意思?对不起,我的英语不好。
int main() {
Reference r;
int arr[3] = {0, 1, 2};
array<const int, Reference::NBITS> resp = r.smaller( arr );
// After get these values I will write it in a file.
return 0;
}