0

So I have a class that uses recursion to generate a level. Simple example of BSP dungeon generation. I'm using the code in here. But, I'm writing in C++ so I can't have a static class.

The problem is I have this, with pointer of the class in there.

#ifndef RECTANGLE_H
#define RECTANGLE_H

class Rectangle
{
public:
    Rectangle(int top, int left, int height, int width);
    ~Rectangle();

    bool split();
    void generate_dungeon();
private:
    static int min_size;
    Rectangle* dungeon;
    Rectangle* left_child;
    Rectangle* right_child;
    int top, left, width, height;
};

#endif

Edit

std::vector<Rectangle*> rectangles;
Rectangle root(0,0,map_height,map_width);

//std::cout<<root.split()<<std::endl;

rectangles.push_back(&root);


//std::cout<<rectangles.at(0).split()<<std::endl;

while(rectangles.size() < 5)
{
    int split_idx = Rng::rand_int(0, (rectangles.size()-1));
    Rectangle* to_split = rectangles.at(split_idx);

    std::cout<<rectangles.size()<<std::endl;
    if((*to_split).split())
    {
        rectangles.push_back(((*to_split)get_left()));
        rectangles.push_back(((*to_split)get_right()));
    }
}
    root.generate_dungeon();

This solves the problem. I call clear on the std::vector after.

Community
  • 1
  • 1
simplicity
  • 117
  • 1
  • 6
  • 1
    left_child is private. I don't think you can access it outside of your class. You would need a method in your class that handles children, or make them public. – Roman Nov 22 '13 at 01:25
  • Roman, but to_split is a member of class Rectangle. Your calling to_split on that and then your accessing .left_child of the Rectangle to_split. Well, that what I think is happening. I suppose I need a copy constructor. – simplicity Nov 22 '13 at 01:29
  • to_split is an instance of a class. The whole point of private fields is to hide information from users. You don't need copy constructor, you need to create a public property that will access your private data ( left, right, etc).. – Roman Nov 22 '13 at 01:42
  • Thanks Roman that seemed to work. I have to check other stuff. Particular about how I'm going to delete the pointer in the class. But, thanks for the help. That seemed to work. – simplicity Nov 22 '13 at 02:02
  • You can add solution to the question and mark it as "answered". But add a solution so that others, who will have a similar problem will benefit form this. :) best of luck – Roman Nov 22 '13 at 02:05

0 Answers0