Sorry, Re- Multi dimensional vector array

I'm trying to assign these classes to my multi-dimension vector array but it doesn't work. I'm getting the error that undeclared pointers can't be assigned. But when I assign my array to declared object it doesn't work.

Extra thing to note, when I stopped making an array of vector pointers (by just making an array instead) it worked. But from advice, I was told that its better to keep (vector)arrays as pointers because pointers use less memory...

I'm planning on storing images into tiles, so a vector array is important. I'm making a side-scroler were I need to create, draw and delete vectors. (thats why the pointers are important)

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
#include<iostream>
#include<vector>

using namespace std;
//initially 9 * 16
#define HEIGHT 9
#define WIDTH 20

class CTile
{
public:
	CTile()
	{
		cout <<"constructor"<<endl;
	}
 virtual void Ooo()
	{
		cout<<"base tile   :  "<<endl;
	}
};











class CTile2: public CTile
{
public:
	CTile2()
	{
		cout <<"constructor"<<endl;
	}
	virtual void Ooo()
	{
		cout<<" tile 2 virtual  :  "<<endl;
	}
};











int main()
{
 CTile *  qA;
	
	 CTile2 * qA2;
	
	vector<vector<CTile *>> qGame_Screen;

	//Set up sizes. (HEIGHT x WIDTH)
	qGame_Screen.resize(HEIGHT);
	for (int i = 0; i < HEIGHT; i++)
		qGame_Screen[i].resize(WIDTH);

	//assigning values to array
	qGame_Screen[0][0] = qA;
	qGame_Screen[0][1] = qA2;

	for (int i = 0;i < HEIGHT;i++)
	{
		for (int j = 1; j < WIDTH; j++)
		{
			qGame_Screen[i][j] = qA2;
			qGame_Screen[i][j]->Ooo();//note array are pointers
		}
	}
	system("PAUSE");
	return 0;
}
I made a code dumping mistake before
Hello,
For the most part, your actual array code is fine. The problem is that you are not creating any instances of your Tile class.

CTile * qA; == A pointer than CAN (but does not) hold an instance of CTile
CTile * qA = new CTile(); == A pointer with an instance created.

Because you're now dynamically allocating memory into a pointer you need to understand how to clean this later, or you'll end up with a memory leak.
http://www.cplusplus.com/doc/tutorial/pointers/
http://www.cplusplus.com/doc/tutorial/dynamic/
To follow on, I suggest you use a shared_ptr<> (www.boost.org) or an auto_ptr<>. Naked pointers (like you're using) will require manually memory de-allocation. Smart pointers do not.
Topic archived. No new replies allowed.