除非同时构造,否则无法在 3D 地图中分配值

Can't assign value in 3d map unless constructing at the same time

本文关键字:3D 地图 分配      更新时间:2023-10-16

我正在尝试创建一个包含我创建的非 stl 类的三维地图。 每当我尝试将实例化变量(我相信在堆栈中)分配给映射时,编译器都会失败。

如果我执行以下操作,它就会起作用:

volume[x][y][z] = Chunk()

但如果我执行,则不会:

Chunk c();
volume[x][y][z] = c;

下面是一个简短的示例:

#include <cstdlib>
#include <map>
#include <stdint.h>
using namespace std;
class Chunk {
public:
    Chunk();
    Chunk(const Chunk& orig);
    virtual ~Chunk();
};
Chunk::Chunk(){
}
Chunk::Chunk(const Chunk& orig) {
}
Chunk::~Chunk() {
}
/*
 *
 */
int main(int argc, char** argv) {
    map<uint32_t, map<uint32_t, map<uint32_t, Chunk > > > volume;
    int32_t width = 5, height = 5, depth = 5;
    for(uint32_t x = 0; x < width; x++){
        for(uint32_t y = 0; y < height; y++){
            for(uint32_t z = 0; y < depth; y++){
                Chunk c();
                volume[x][y][z] = c;
            }
        }
    }
    return 0;
}

此示例给出以下编译错误:

g++    -c -g -MMD -MP -MF build/Debug/GNU-Linux-x86/main.o.d -o build/Debug/GNU-Linux-x86/main.o main.cpp
main.cpp: In function ‘int main(int, char**)’:
main.cpp:41:35: error: no match for ‘operator=’ in ‘(&(& volume.std::map<_Key, _Tp, _Compare, _Alloc>::operator[] [with _Key = unsigned int, _Tp = std::map<unsigned int, std::map<unsigned int, Chunk> >, _Compare = std::less<unsigned int>, _Alloc = std::allocator<std::pair<const unsigned int, std::map<unsigned int, std::map<unsigned int, Chunk> > > >, std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type = std::map<unsigned int, std::map<unsigned int, Chunk> >, std::map<_Key, _Tp, _Compare, _Alloc>::key_type = unsigned int]((*(const key_type*)(& x))))->std::map<_Key, _Tp, _Compare, _Alloc>::operator[] [with _Key = unsigned int, _Tp = std::map<unsigned int, Chunk>, _Compare = std::less<unsigned int>, _Alloc = std::allocator<std::pair<const unsigned int, std::map<unsigned int, Chunk> > >, std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type = std::map<unsigned int, Chunk>, std::map<_Key, _Tp, _Compare, _Alloc>::key_type = unsigned int]((*(const key_type*)(& y))))->std::map<_Key, _Tp, _Compare, _Alloc>::operator[] [with _Key = unsigned int, _Tp = Chunk, _Compare = std::less<unsigned int>, _Alloc = std::allocator<std::pair<const unsigned int, Chunk> >, std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type = Chunk, std::map<_Key, _Tp, _Compare, _Alloc>::key_type = unsigned int]((*(const key_type*)(& z))) = c’
main.cpp:41:35: note: candidate is:
main.cpp:14:7: note: Chunk& Chunk::operator=(const Chunk&)
main.cpp:14:7: note:   no known conversion for argument 1 from ‘Chunk()’ to ‘const Chunk&’
make[2]: *** [build/Debug/GNU-Linux-x86/main.o] Error 1
make[2]: Leaving directory `/home/sean/NetBeansProjects/test3dmap'
make[1]: *** [.build-conf] Error 2
make[1]: Leaving directory `/home/sean/NetBeansProjects/test3dmap'
make: *** [.build-impl] Error 2

BUILD FAILED (exit value 2, total time: 213ms)

怀疑我可能不得不创建一个 3D 地图,如下所示:

map<uint32_t, map<uint32_t, map<uint32_t, Chunk* > > > volume;

但是我将不得不在我自己的代码中执行内存管理,我想避免这种情况。 如何使显示的地图示例正常工作?

Chunk c();是一个

函数声明。试试Chunk c; .

附言另请参阅 http://en.wikipedia.org/wiki/Most_vexing_parse(正如乍得在上面链接中的回答中所指出的那样,这并不完全相同,但也很高兴知道)。

请参阅C++常见问题解答。 矩阵使用()[]更好地开发。
这是链接:C++常见问题解答:矩阵运算符重载