除非同时构建,否则无法在3d地图中指定值

时间:2012-11-30 21:34:23

标签: c++ class stl map constructor

我正在尝试创建一个包含我创建的非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)

我怀疑我可能不得不创建如下的三维地图:

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

然后我将不得不在我自己的代码中执行内存管理,我想避免。如何让地图示例显示出来?

2 个答案:

答案 0 :(得分:3)

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

P.S。另见http://en.wikipedia.org/wiki/Most_vexing_parse(正如乍得在上面的链接中的答案中所指出的那样,这并不完全相同,但也很有用)。

答案 1 :(得分:1)

请参阅C ++ FAQ。使用()而不是[]可以更好地开发矩阵 这是链接:C++ FAQ: Matrix operator overloading