为什么我的C++映射实现不存储值?

Nic*_*ton 1 c++ map

我有一个名为ImageMatrix的类,它以递归方式实现C++映射; 最终结果是我有一个3维数组.

typedef uint32_t VUInt32;
typedef int32_t VInt32;

class ImageMatrix
{
public:
    ImageMatrixRow operator[](VInt32 rowIndex)
private:
    ImageMatrixRowMap rows;
};

typedef std::map <VUInt32, VInt32> ImageMatrixChannelMap;

class ImageMatrixColumn
{
public:
    VInt32 &operator[](VUInt32 channelIndex);
private:
    ImageMatrixChannelMap channels;
};

typedef std::map<VUInt32, ImageMatrixColumn> ImageMatrixColumnMap;

class ImageMatrixRow
{
public:
    ImageMatrixColumn operator[](VUInt32 columnIndex);
private:
    ImageMatrixColumnMap columns;
};

typedef std::map<VUInt32, ImageMatrixRow> ImageMatrixRowMap;
Run Code Online (Sandbox Code Playgroud)

每个运算符只返回一个map-wrapper类,如下所示:

ImageMatrixRow ImageMatrix::operator[](VInt32 rowIndex)
{
    return rows[rowIndex];
}

ImageMatrixColumn ImageMatrixRow::operator[](VUInt32 columnIndex)
{
    return columns[columnIndex];
}

VInt32 &ImageMatrixColumn::operator[](VUInt32 channelIndex)
{
    return channels[channelIndex];
}
Run Code Online (Sandbox Code Playgroud)

基本上,当我将值设置为100,并将值测试为cout时,它显示为0,而不是我设置它的数字.

for (VUInt32 a = 0; a < GetRowCount(); a++)
{
    for (VUInt32 b = 0; b < GetColumnCount(); b++)
    {
        for (VUInt32 c = 0; c < GetChannelCount(); c++)
        {
            VInt32 value = 100;
            matrix[a][b][c] = value;

            VInt32 test = matrix[a][b][c];

                            // pixel = 100, test = 0 - why?
            cout << pixel << "/" << test << endl;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

注意:我已经更改了此示例的原始代码,因此占用的空间更少,因此可能会出现一些语法错误(请不要指出它们).

dir*_*tly 5

以下运算符按值返回,没有写入修改实际数据.

ImageMatrixRow ImageMatrix::operator[](VInt32 rowIndex);

ImageMatrixColumn ImageMatrixRow::operator[](VUInt32 columnIndex);
Run Code Online (Sandbox Code Playgroud)

使用:

ImageMatrixRow& ImageMatrix::operator[](VInt32 rowIndex)


ImageMatrixColumn& ImageMatrixRow::operator[](VUInt32 columnIndex)
Run Code Online (Sandbox Code Playgroud)