array of structures

Started by
7 comments, last by Joshnathan 19 years, 4 months ago
Hi all, I want to make an array of structures, to represent my world. This is my code atm:

struct MAP[40][30]
    {
           SDL_Surface *background;
           int value;
    };
    
int main()
{
    InitSDL();
    MAP map;
    
    SDL_Surface *temp = IMG_Load("map.gif");
    for(int i = 0; i <= 40; i++)
    {
       for(int j = 0; j <= 30; j++)
       {
               map[j].background = temp;    
               map[j].value = 0;
       }      
    }
    
    return 0;
}

I get many errors wen I try to compile this. some are: 14 C:\WINDOWS\Desktop\Joshua\RPG\main.cpp syntax error before `[' token 18 C:\WINDOWS\Desktop\Joshua\RPG\main.cpp syntax error before `}' token 23 C:\WINDOWS\Desktop\Joshua\RPG\main.cpp aggregate `MAP map' has incomplete type and cannot be defined Any idea what I am doing wrong?
-----------------------------Sismondi GamesStarted c++ in October 2004...
Advertisement
You have the right idea but what you want to do is define the structure first,

typedef struct _MAP{    SDL_Surface *background;    int value;}MAP ;


and then define the multi-array.

MAP  foo[10][20];...


Hope that helps...
"C and C++ programmers seem to think that the shortest distance between two points is the great circle route on a spherical distortion of Euclidean space."Stephen Dewhurst
it helps a lot! :D
I have on little problem still though.
These are my two files:
main.cpp
#include "map.h"void InitSDL(){     if(SDL_INIT_VIDEO < 0)     cerr << "Error Initiating SDL..." << SDL_GetError();     SDL_Surface *screen = SDL_SetVideoMode(800, 600, 32, SDL_HWSURFACE|SDL_DOUBLEBUF);     atexit(SDL_Quit);}    int main(int argc, char *argv[]){    InitSDL();        InitMap();        return 0;}


and map.h
#ifndef MAP_H#define MAP_H#include <SDL/SDL.h>#include <SDL/SDL_image.h>#include <iostream>using namespace std;typedef struct _MAP    {           SDL_Surface *background;           int value;    }MAP;     void InitMap(){    MAP map[40][30];    SDL_Surface *temp;    temp = IMG_Load("map.gif");    for(int i = 0; i <= 40; i++)    {       for(int j = 0; j <= 30; j++)       {               map[j].background = temp;                   map[j].value = 0;       }          }}#endif


It compiles ok, but when I run the program, my computer tells me "RPG" has made an error in <unknown> "RPG" will be terminated...

Anyone know why?

//Edit: I also get these 3 compile errors:
1 C:\WINDOWS\Desktop\Joshua\RPG\main.cpp In file included from main.cpp
7 C:\WINDOWS\Desktop\Joshua\RPG\map.h:31 [Warning] no newline at end of file
2 C:\WINDOWS\Desktop\Joshua\RPG\main.cpp:18 [Warning] no newline at end of file

I can't make any sense out of them, can anyone?
-----------------------------Sismondi GamesStarted c++ in October 2004...
Heya

Ok first up the 3 compile errors can be easily fixed by hitting enter at the end of each file. Next up you dont actually initialize SDL anywhere in your code the error will be caused by trying to use an sdl function without it being initialized. try replacing if(SDL_INIT_VIDEO < 0) in your InitSDL() function with this if(SDL_Init(SDL_INIT_VIDEO) < 0) and you'll find it works better hopefully, you'll still get the error if sdl doesnt init, but i'll let you try and nut that one out for yourself

I hope that help and if I did screw it up feel free to yell at me

Later days
Dex
Quote:Original post by Joshnathan
it helps a lot! :D


np - thanks.

Quote:Original post by Joshnathan
I have on little problem still though.

//Edit: I also get these 3 compile errors:
1 C:\WINDOWS\Desktop\Joshua\RPG\main.cpp In file included from main.cpp
7 C:\WINDOWS\Desktop\Joshua\RPG\map.h:31 [Warning] no newline at end of file
2 C:\WINDOWS\Desktop\Joshua\RPG\main.cpp:18 [Warning] no newline at end of file

I can't make any sense out of them, can anyone?


Well - I dont think you are getting a clean compile here so we should probably fix that first.

First - I am not much an expert on SDL, but there are a few things that seem odd to me (but someone familiar with SDL could give more conclusive advice). Just a few observations:

First, I would make it a habit to not put includes in include files. Keep your includes in the code files only. This creates some hassles sometimes with the proper order of the includes but you will thank me when your projects get larger - your compile times will drop significantly.

Second, is SDL_INIT_VIDEO a macro or is it just a constant. I guess I am unsure how SDL actually gets initialized here. I reorgaized the files a bit and they compile (with my SDL hacks in place) but you may need the help of an SDL pro to figure out the runtime issues.

Third, even if you get an error on SDL_INIT_VIDEO not working, you are still going to try and init a surface, as well as processing InitMap... so I am going to assume that something didnt process and the next step that was dependent puked.

Also - arrays go from 0..n-1, so if you allocate 10 elements you want to go from 0..9 therefore keep your check to n-1.

I re-wrote this a bit(while troubleshooting - nothing personal...) see how that works...

map.cpp
#include <SDL/SDL.h>#include <SDL/SDL_image.h>#include <iostream>#include "map.h"using namespace std;bool InitSDL(){     if(SDL_INIT_VIDEO < 0)	 {		cerr << "Error Initiating SDL..." << SDL_GetError();		return false;	 }	 SDL_Surface *screen = SDL_SetVideoMode(800, 600, 32, SDL_HWSURFACE|SDL_DOUBLEBUF);     atexit(SDL_Quit);	 return true;}    bool InitMap(){    MAP map[40][30];    SDL_Surface *temp = NULL;    temp = IMG_Load("map.gif");    if( NULL == temp )        return false;    for(int i = 0; i < 40; i++)    {       for(int j = 0; j < 30; j++)       {               map[j].background = temp;                   map[j].value = 0;       }          }	return true;}int main(int argc, char *argv[]){	if( true == InitSDL() )	{		if( true == InitMap() )		{			//	.. additional processing...		}  	}    //  Keep in mind that if InitSDL succeeds and InitMap fails then you need to clean some things up...     return 0;}


map.h
#ifndef MAP_H#define MAP_Htypedef struct _MAP{	SDL_Surface *background;    int			value;}MAP;     #endif



let me know how that goes....
"C and C++ programmers seem to think that the shortest distance between two points is the great circle route on a spherical distortion of Euclidean space."Stephen Dewhurst
thanks a lot, both of you, but you were both wrong.
The problem was that I initiated an array:
map[40][30], and then I was loading images with the loop like:
SDL_Surface *temp = IMG_Load("map.gif");    for(int i = 0; i <= 40; i++)    {       for(int j = 0; j <= 30; j++)       {               map[j].background = temp;                   map[j].value = 0;       }          }


the prob was that map[40][30] doesn't exist, I forgot to count the [0][0]. so now I changed the code to:
SDL_Surface *temp = IMG_Load("map.gif");    for(int i = 0; i < 40; i++)    {       for(int j = 0; j < 30; j++)       {               map[j].background = temp;                   map[j].value = 0;       }          }

and everything works fine! :D
-----------------------------Sismondi GamesStarted c++ in October 2004...
It looks to me like xiuhcoatl picked up on the for loop bounds problem. It wasn't very tactful to tell him he was wrong.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
Don't use 'typedef struct' in C++; it is a C idiom used to work around a limitation in that language.

struct foo {  int x;}foo f; // OK in C++. Not OK in C.struct foo f; // You need this in C.typedef struct foo_struct {  int x;} foo;foo f; // Now this is ok in both C and C++.// However, you did extra work for C++ that is not needed. Cut it out.

Quote:Original post by iMalc
It looks to me like xiuhcoatl picked up on the for loop bounds problem. It wasn't very tactful to tell him he was wrong.


I appreciate their help very much, it wasn't meant to be an insult...
-----------------------------Sismondi GamesStarted c++ in October 2004...

This topic is closed to new replies.

Advertisement