Dynamic Allocation Issue

Hi, I am working on a project for school, and I have run into a problem. I am trying to create a dynamically allocated array of Class objects, which is determined through file reading, and I can't get it to work.
The Boss and Enemy classes inherit from the Location class.
Also, in this particular class file, my compiler is underlining = and << . I used #include <iostream> and using namespace std; so there must be some other problem.
If you can fix any errors I would really appreciate it. Thanks.
header file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#ifndef GAME_H
#define GAME_H
#include "location.h"
#include "player.h"
#include <fstream>
#include <iostream>
using namespace std;
class Game
{
private:
	int rows,cols;
	char c;
	Location **arr;
public:
	Game();
	void playGame (Player& p, Location& l);
	void getGrid();
	void displayGrid();
};

#endif 

.cpp file
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
void Game::getGrid()
{
	ifstream inStream;
	int rows, cols;
	inStream.open("game.txt");
	if (!inStream)
		cout << "File couldn't be opened!"<<endl;
	system("pause");
	while (!inStream.eof())
	{
		inStream >> rows >> cols;
		for (int i=0; i<rows; i++)
		{
			for (int j=0; j<cols; j++)
			{
				arr[i][j]=new Location l;
			}
		}
		for (int i=0; i<rows; i++)
		{
			for (int j=0; j<cols; j++)
			{
				inStream >> c;
				if (c=='L')
					arr[i][j]=new Location l;
				if (c=='B')
					arr[i][j]=new Boss b;
				if (c=='E')
					arr[i][j]=new Enemy e;
			}
		}
		break;
	}
}


oh, and the file i'm reading from looks like this currently.

5 5
L L L E L
L L E L L
L L L L L
L E L E L
L L L L B
Last edited on
I forgot to mention that this project is due at midnight tonight, so time is really an issue here.
What are the errors you are getting?
I am getting C2679, C2146, C2065, and C2061.

And they are happening on lines 25-29.
To make the question more specific... if i initialize an 2d array of pointers. like this Location **arr2;

And I try to define it like this.
1
2
3
4
5
6
7
for (int i=0; i<rows; i++)
	{
		for (int j=0; j<cols; j++)
		{
			arr2[i][j]=new char l;
		}
	}


What am I doing wrong?
If I was your teacher I would be pissed that your living things are locations. How do they move? Living things have locations.

Anyway, here is how to create a 2d array:
http://www.cplusplus.com/forum/general/9508/

Read the whole thread, "Tristanm is correct".
Last edited on
Thanks a lot for the help, it fixed most of my problems. I just have one last question. In this function, I have allocated the space for the 2d array and defined each pointer, but I want to go through the array and change what default object some of the pointers are pointing at based on information from a text file. I am trying to convert a Location pointer to a Enemy, Boss, or Riddles pointer (Enemy, Boss, and Riddle inherit from Location)

here is my modified function

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
void Game::getGrid()
{
	ifstream inStream;
	int rows, cols;
	inStream.open("game.txt");
	if (!inStream)
		cout << "File couldn't be opened!"<<endl;
	system("pause");
		inStream >> rows >> cols;
	arr2 = new char *[rows];
	for (int i=0; i<rows; i++)
	{
		arr2[i]=new char [cols];
	}

	arr = new Location *[rows];
	for (int i=0; i<rows; i++)
	{
		arr[i]=new Location [cols];
	}


	for (int i=0; i<rows; i++)
	{
		for (int j=0; j<cols; j++)
		{
			inStream >> arr2[i][j];
			if (arr2[i][j]=='B')
					arr[i][j]=new Boss b;
			if (arr2[i][j]=='E')
					arr[i][j]=new Enemy e;
			if (arr2[i][j]=='R')
					arr[i][j]=new Riddles r;

		}
		cout << endl;
	}
	system("pause");
}


the part that doesn't work is on lines 28-33
Last edited on
Topic archived. No new replies allowed.