Vector Container question

Hi all,

Just a quick one. I am writing a simple game where boxes will bounce around the screen and the player has to dodge them while to number of boxes increase and bounce all over the place. I can't put my structure into an array correctly. Help please?

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
 #include <stdlib.h> //rand() and srand() use in main.
#include<allegro5/allegro.h> // Main Allegro header
#include<allegro5/allegro_native_dialog.h> // For native to OS message boxes
#include<allegro5/allegro_ttf.h> //  Ability to laod true type fonts
#include<allegro5/allegro_font.h> // Needed for font functions
#include<allegro5/allegro_primitives.h> //  Needed to draw shapes to window  must call the correct init before use.
#include<time.h> // Need to set the seed for the randomized timer 
#include<iostream>  // incase I want to use cout << for debugging to the consolse window
#include<string.h>  //Best container class to use instead of standard for string.  Use of String function built in.
#include<vector> // Choosen Container Class will be using pushback etc to add and remove boxes.

using namespace std;

const int numofboxes = 1; // Initial Number of boxes 1 Array start from 0

typedef struct boxes  //my structure that I want to attach to my vector container / array
{
	float red; // RGB colour of box.
	float green;
	float blue;
	float x; // X , y coords of box top left corner
	float y;
	float boxsize; //size
	float dx; //  Box direction x
	float dx; // Box direction x
	float Speed; // Amount of pixel the box will move
};
 vector<boxes>box[numofboxes]; 

box[0].x = 1;
I can't put my structure into an array correctly.

Why not?

BTW, line 28 is creating an array of vectors.

In C++, it's not necessary to typedef your struct. boxes becomes a typename automatically.

To add a box, you want to do the following:
1
2
3
4
5
    vector<boxes> v_boxes; 
    boxes  box;

    box.x = 1;
    v_boxes.push_back (box);

Sorry guys,

I have sorted now with a bit of searching I should have done and the vector and resize has to be in main:
1
2
3
4
5
	
int main()
{
    vector<boxes> box;
    box.resize(numofboxes);

....
[/code]

Delete or move if you want admin unless this is useful for others.
Topic archived. No new replies allowed.