Program ends halfway after implementation of polymorphism

I'm trying to implement polymorphism in the code below, it works fine without polymorphism(calling with b.function), in this case, it stops at the function fixShape(which writes 3 squares of length 3 into the 2d vector box), just before calling function boundaryCheck(so that the squares don't go over the desired size of 2d vector box),
unoccupiedSpace stores places that are still '.' so after taking the next random number to place the squares will not repeat.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
  #include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <vector>
#include <sstream>
#include <memory>

using namespace std;

typedef vector<char> vchar;
typedef vector<int> vint;

class Shape
{
	protected:
	int instructionList[10];
	int rowPoint;
	int colPoint;
	int colAddress;
	int seed;
	vector<vchar> box;
	vector<vint> unoccupiedSpots;
	
	public:
	virtual void fixShape() {};
	Shape();
	virtual ~Shape();
	void setInstruction();
	void setBox();
	void randRow(int &r, int &seed, vector<vint> vect);
	void randCol(int &c, int &add, int n, vector<vint> vect);
	void TEST();
	bool boundaryCheck(int shapeHeight, int shapeLength, int randH, int randL);
};

Shape::Shape() : seed(0)
{}

Shape::~Shape()
{}

void Shape::setInstruction()				//Instruction List[i]:
{											// 1. Length of Box [0]
	int temp;								// 2. Height of Box [1]
	ifstream inFile;						// 3. Maximum number of attempts [2]
	inFile.open("Batch.txt");				// 4. Number of Squares [3]
											// 5. Length of Square [4]
	if (!inFile) 							// 6. Number of Rectangles [5]
	{										// 7. Length of Rectangle [6]
		cerr << "Unable to open file.";		// 8. Height of Rectangle [7]
		exit(1);							// 9. Number of Triangles [8]
	}										// 10. Height of Triangle [9]
	
	for(int i=0;i<10;i++)
		inFile >>instructionList[i];
	
	inFile.close();
}

void Shape::randRow(int &r, int &seed, vector<vint> vect)
{
	srand(seed++);
	r=rand()%vect.size();
}

void Shape::randCol(int &c, int &add, int n, vector<vint> vect)
{
	srand(1);
	add=rand()%vect[n].size();
	c=vect[n][add];
}

void Shape::setBox()
{
	vchar tempchar;
	vint tempint;
	
	for(int i=0;i<instructionList[0];i++)
	{
		tempchar.push_back('.');
	}
	for(int j=0;j<instructionList[1];j++)
	{
		box.push_back(tempchar);
	}
		
	for(int j=0;j<instructionList[0];j++)
	{
		tempint.push_back(j);
	}
	for(int i=0;i<instructionList[1];i++)
	{
		unoccupiedSpots.push_back(tempint);
	}

}


bool Shape::boundaryCheck(int shapeHeight, int shapeLength, int randH, int randL)
{
	if(box[shapeHeight+randH][shapeLength+randL]=='.')		//check if shape will be out of bound by its bottom rightmost point
	{	
		return true;
	}
	else
	{
		int a=shapeHeight+randH;
		unoccupiedSpots[a].erase(unoccupiedSpots[a].begin()+shapeLength+randL);
		return false;
	}
}
void Shape::TEST()
{
	for(int i=0;i<box.size();i++)
	{
		for(int j=0;j<box[i].size();j++)
		{
			cout<<box[i][j];
		}
		cout<<endl;
	}
}

class Square : public Shape
{
	public:
	void fixShape() 
	{	
		cout<<"Square here\n";
		
		for(int i=0;i<3;i++)
		{
			randRow(rowPoint, seed, unoccupiedSpots);
			randCol(colPoint, colAddress, rowPoint, unoccupiedSpots);
cout<<"Program stops here";
			if(boundaryCheck(3,3,rowPoint,colAddress))
			{
				for(int i=0;i<3;i++)
					for(int j=0;j<3;j++)
					{
						box[rowPoint+i][colPoint+j]='A';
						unoccupiedSpots[rowPoint+i].erase(unoccupiedSpots[rowPoint+i].begin()+colAddress);
					}
			}
		}
	}
};

int main()
{
	Square b;
	Shape a;
	 
	Shape *Shape1=&b;
	 
	Shape1->setInstruction();
	Shape1->setBox();
	Shape1->fixShape();
	Shape1->TEST();
	
	
	
	return 0;
}
What platform are you on? You should use a debugger to help solve issues like these.

Also, only call srand one time, at the beginning of your program. Unless you're purposefully trying to reset the randomness every iteration.

1
2
3
4
			randRow(rowPoint, seed, unoccupiedSpots);
			randCol(colPoint, colAddress, rowPoint, unoccupiedSpots);
cout<<"Program stops here";
			if(boundaryCheck(3,3,rowPoint,colAddress))

If the program is "stopping" (crashing) around here, then use a debugger and put breakpoints on these lines, and then step through them line by line, looking at current variable values to see if anything looks amiss.

My guess is that line 102 is accessing out-of-bounds array indices.
box[shapeHeight+randH][shapeLength+randL]
You might want to look closer at the values going into this call.

Since box is a vector<vector<char>>, you can use the .at() method instead of using [], because [] doesn't do bounds checking.

box.at(shapeHeight+randH).at(shapeLength+randL) 
will throw an exception if it is out of bounds.

Last edited on
Thanks for the reply, I'll check them through, as for srand, 3 of the squares were sticking together continuously down the row in the same column, no overlapping though, and one of the random number which is only generated once in one loop seems to be repeating.

Platform would be mingw if it's the thing you're referring to.
Last edited on
re srand: Yes, but is that the behavior you want? srand is normally only called once at initialization of a program, unless the programmer intentionally wants to only use a certain seed for testing purposes. The state of rand() is reset after srand() is called, so by repeatedly calling srand, you keep resetting the state of rand(), which I would assume is not intended behavior.

e.g. to make the seed based on the current time when the program is started
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <cstdlib>
#include <ctime>

int foo()
{
    // (no need to call srand here)
    return rand();
}

int main()
{
    srand(time(nullptr)); // only time you ever need to call srand

    rand();
    rand();
    rand();
    foo();
    // ...
}


mingw
That's the compiler (GCC for Windows), so you might have a thing called GDB included with your compiler package. Try searching for it.
https://en.wikipedia.org/wiki/GNU_Debugger

If you're using an IDE, it probably has Debugging functionality built into it.
Last edited on
Fixed the error, many thanks.
Topic archived. No new replies allowed.