gpt4 book ai didi

c++ - 除非同时构建,否则无法在 3d map 中分配值

转载 作者:行者123 更新时间:2023-11-30 02:01:57 25 4
gpt4 key购买 nike

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

如果我执行它就有效:

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 :

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

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

最佳答案

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

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

关于c++ - 除非同时构建,否则无法在 3d map 中分配值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13653520/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com