Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/138.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 无法将第二个下标运算符重载标记为常量_C++_Matrix_Multidimensional Array - Fatal编程技术网

C++ 无法将第二个下标运算符重载标记为常量

C++ 无法将第二个下标运算符重载标记为常量,c++,matrix,multidimensional-array,C++,Matrix,Multidimensional Array,所以在我的Matrix类中,我在post中使用了一些奇怪的语法,以便将该类用作2D数组。然而,将第二个重载标记为const只是告诉我它应该是一个“;” #pragma一次 #包括 使用std::ostream; 结构矩阵{ 公众: 矩阵(浮点单位=0.0f){ 对于(int行=0;行

所以在我的Matrix类中,我在post中使用了一些奇怪的语法,以便将该类用作2D数组。然而,将第二个重载标记为const只是告诉我它应该是一个“;”

#pragma一次
#包括
使用std::ostream;
结构矩阵{
公众:
矩阵(浮点单位=0.0f){
对于(int行=0;行<4;++行)
for(int列=0;列<4;++column)
矩阵[行][列]=(行==列)?标识:0.0f;
}
私人:
枚举{
行=4,
列=4
};
浮动矩阵[行][列];
公众:
浮点(&运算符[](无符号整数索引))[列]{
返回矩阵[索引];
}
//不让我把它标记为const
浮点(&运算符[](无符号整数索引))[Columns]常量/“应为“;””{
返回矩阵[索引];
}
friend ostream&operator正确的语法是

const float (&operator[](unsigned int index) const) [Columns] 
您可以从
运算符[](unsigned int index)const
开始,然后在其周围添加结果类型(如果您喜欢这种类型的话)

但为什么要让生活变得艰难呢?
使用类型别名

using Row = float[Columns];
Row& operator[](unsigned int index);
const Row& operator[](unsigned int index) const;
正确的语法是

const float (&operator[](unsigned int index) const) [Columns] 
您可以从
运算符[](unsigned int index)const
开始,然后在其周围添加结果类型(如果您喜欢这种类型的话)

但为什么要让生活变得艰难呢?
使用类型别名

using Row = float[Columns];
Row& operator[](unsigned int index);
const Row& operator[](unsigned int index) const;

这些不是函数重载,而是变量声明..或者我甚至不确定。这可能不是你想要做的。你想实现什么?@JHBonarius我只是想能够使用[][]在常量矩阵对象上。它适用于非常量对象,但对于常量它只会抛出一个错误。这些不是函数重载,而是变量声明..或者我甚至不确定。这可能不是你想要做的。你想实现什么?@JHBonarius我只想能够使用[][]在常量矩阵对象上。它适用于非常量对象,但对于常量,它只会抛出一个错误。