除非同时构造,否则无法在 3D 地图中分配值
Can't assign value in 3d map unless constructing at the same time
我正在尝试创建一个包含我创建的非 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++常见问题解答:矩阵运算符重载
相关文章:
- 为什么不;名字在地图上是按顺序排列的吗
- 无法将结构注册为增强几何体3D点
- 基于多个条件处理地图中的所有元素
- 在C++中将矢量转换为嵌套地图
- OpenGL大的3D纹理(>2GB)非常慢
- 是什么导致了Unity 3D中的"错误线程异常"?
- 替换基于地图的所有引用
- 如何区分地图中的 0 和 false?
- 地图计数确实很重要,或者只是检查是否存在
- 如何声明一个可以在整个程序中使用的全局 2d 3d 4d .. 数组(堆版本)变量?
- 如何从地图中删除矢量对象
- 是否有任何C++功能可以对地图进行排序?
- 如何使用 std::variant 打印地图键/值?
- 有没有办法从点云和深度数据构建和存储3D地图
- 开放世界 3D 环境的最佳地图格式
- 除非同时构造,否则无法在 3D 地图中分配值
- 我应该使用地图、3D 数组还是坚持使用堆栈
- 如何从给定的3D地图值获得所有的键
- 插入和读取值从3D地图
- 在3D地图中插入值