简体   繁体   English

C ++我在数组/动态内存方面遇到一些问题

[英]C++ I'm having some problems with arrays/dynamic memory

so I'm making a lame little snowboarding game (which is what all my questions have been about) and I'm having some issues. 所以我正在制作一个a脚的小单板游戏(这是我所有问题都想解决的问题),并且遇到了一些问题。 I have a Biome class, which has a dynamic array to store the possible obstacles for that biome (obstsInBiome). 我有一个生物群系类,该类具有动态数组来存储该生物群系的可能障碍(obstsInBiome)。 Here is the constructor: 这是构造函数:

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];
    }
}

Then to create a new biome, i use this: 然后创建一个新的生物群系,我用这个:

Obstacle villageObsts[] = {tree, rock, cabin, log}; // tree, rock, and cabin are all Obstacles
Biome village(villageObsts, 4);

Somewhere within this code, the first element of obstsInBiome is not getting set properly. 在此代码中的某处,obstsInBiome的第一个元素未正确设置。 village.obstsInBiome[0] is what I mean. village.obstsInBiome[0]是我的意思。

When i try to draw that to the screen, it doesn't appear and invisible collisions happen with the player as if they hit the obstacle. 当我尝试将其绘制到屏幕上时,它没有出现,并且与玩家发生了看不见的碰撞,好像它们撞到了障碍物一样。 The rest of the array (rock, cabin, and log) all work perfectly. 阵列的其余部分(岩石,小屋和原木)都可以正常工作。 village.obstsInBiome[1 through 3] all work fine. village.obstsInBiome[1 through 3]一切正常。

Can someone point out the error in this code? 有人可以指出此代码中的错误吗?

At first glance, in particular at where you say you think the error is, it could be in the implementation of the copy constructor for the tree. 乍一看,特别是在您认为错误所在的位置,这可能是在树的复制构造函数的实现中。 I don't know what exactly an "Obstacle" is, nor do I know what the specific characteristics of those 4 instances are. 我不知道“障碍”到底是什么,也不知道这四个实例的具体特征是什么。 Perhaps the tree instance has some kind of data that doesn't copy cleanly, whereas the other 3 do. 也许树实例具有某种不能干净复制的数据,而其他3种却没有。

If you aren't sure what I mean by the copy constructor, that is probably a good thing to learn about. 如果您不确定复制构造函数的含义,那可能是一件好事。

http://en.wikipedia.org/wiki/Copy_constructor http://en.wikipedia.org/wiki/Copy_constructor

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM