C++ 我在数组/动态内存方面遇到了一些问题
C++ I'm having some problems with arrays/dynamic memory
所以我正在做一个蹩脚的小滑雪游戏(这就是我所有问题的内容),我遇到了一些问题。我有一个生物模型类,它有一个动态数组来存储该生物模型可能存在的障碍物(obstsInBiome)。这是构造函数:
Biome::Biome(Obstacle obsts[], int amountOfObsts)
{
maxObstAmount = 10; // Max amount of obstacles to spawn in each biome
obstAmount = amountOfObsts; // The amount of obstacles passed in in the obsts parameter
// This part copys the array passed in to the obstsInBiome array (Class member to store obstacles)
// I think this is where the error may be
obstsInBiome = new Obstacle [amountOfObsts]; // Creating array to hold the possible obstacles in this biome
for (int x = 0; x < amountOfObsts; x++) // Filling the obstacle array with the obstacles passed in
{
obstsInBiome[x] = obsts[x];
}
}
然后创建一个新的生物群落,我使用这个:
Obstacle villageObsts[] = {tree, rock, cabin, log}; // tree, rock, and cabin are all Obstacles
Biome village(villageObsts, 4);
在这段代码的某个地方,obstsInBiome的第一个元素没有正确设置。village.obstsInBiome[0]
就是我的意思。
当我试图把它画到屏幕上时,它不会出现,玩家会发生无形的碰撞,就好像他们撞到了障碍物一样。阵列的其余部分(岩石、小屋和原木)都能完美工作。village.obstsInBiome[1 through 3]
运行良好。
有人能指出这段代码中的错误吗?
乍一看,特别是在你认为错误的地方,它可能是在树的复制构造函数的实现中。我不知道"障碍"到底是什么,也不知道这4个例子的具体特征是什么。也许树实例中有某种数据没有完全复制,而其他3个实例中有
如果你不确定我所说的复制构造函数是什么意思,那可能是一件值得学习的好事。
http://en.wikipedia.org/wiki/Copy_constructor
相关文章:
- 编译包含字符串的代码时遇到问题
- 在顶点着色器中使用 OpenGl 的未声明标识符,我在顶点着色器中绘制三角形时遇到问题
- 从其他类访问类时遇到问题
- 使用静态成员声明类时遇到问题
- C++ 填充数组时遇到问题
- 在将字符串放入字符串向量时遇到问题?
- 余数除法和不允许除以零 (c++) 时遇到问题
- 我在C++中使用 getline() 函数时遇到问题
- C++线程安全:如果只有一个线程可以写入非原子变量,但多个线程从中读取. 会遇到问题吗?
- 在我的四叉树实现中遇到问题
- 在 c++ 中循环遍历数组时遇到问题
- 使用 MinGW 在 Windows 上构建 GLEW 时遇到问题
- 编译 Cilk Plus 程序时遇到问题
- 存储另一个函数返回的布尔数组时遇到问题
- 我在 OpenGL 中显示图像时遇到问题
- 我在解决此错误时遇到问题.我正在努力在主函数中传递数组
- 在 for 循环中更新两个变量时遇到问题C++
- 为什么我的程序在打开网络设备时遇到问题
- 在 Tensorflow C++ 中使用 FIFOQueue 时遇到问题
- 为什么我在虚幻引擎中的多态性和接口方面遇到问题?