1

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

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

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;

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

4

2 回答 2

3

Chunk c();是一个函数声明。试试Chunk c;

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

于 2012-11-30T21:39:30.150 回答
1

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

于 2012-11-30T21:42:23.700 回答