0

so I'm making a little snowboarding game. I have a class for obstacles called Obstacle. Now i'm creating a Biome class so I can randomly generate some (2D) terrain. Here is the definition of the Biome class:

class Biome
{ 
private:
    Obstacle * obstsInBiome;
public:
    Biome(Obstacle obsts[], int amountOfObsts);
    ~Biome();
    void draw(); // Draw the biome
};

Then here's the constructor

Biome::Biome(Obstacle obsts[], int amountOfObsts)
{
    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];
    }
}

When I'm trying to allocate the memory to the array obstsInBiome = new Obstacle [amountOfObsts]; I get the error: "no matching function for call to Obstacle::Obstacle()"

Is it not possible to dynamically allocate memory for objects? Can someone suggest a way to store the possible objects in each biome?

Any help is appreciated, thanks

picklechips
  • 746
  • 2
  • 8
  • 28
  • Since you have (presumably) defined a constructor for `class Obstacle`, the compiler doesn't automatically define the default constructor (with no parameters). You need to do this yourself. – Matt Dec 02 '13 at 22:38
  • Hint: Try to default construct an `Obstacle`. You will see it has nothing to do with dynamic allocation. Try `Obstacle o;` and see. – juanchopanza Dec 02 '13 at 22:38
  • 2
    In general the design misses [rule-of-three](http://stackoverflow.com/questions/4172722/what-is-the-rule-of-three) – πάντα ῥεῖ Dec 02 '13 at 22:47
  • Ahhhh thanks guys for your very quick and correct responses! I'm glad it was such a simple solution, I appreciate your help! Everything works great now! Edit: And also, thanks for that link g-makulik, looks like it'll be a good and helpful read! – picklechips Dec 02 '13 at 22:53

0 Answers0