Interdependant Headers
SkaxCo

I am trying to make my first game with more than one header. I have: main.cpp, mmm_aux, and mmm_drawing. Aux is globals, consts, and other trashy one-use functions. Drawing, is, well, drawing. I need to use mmm.dat, declared in Aux (globally declared) in Drawing. My includes in main are:

/*INCLUDES*/
#include <cmath>
#include <ctime>
#include <string.h>
#include "mmm_aux.h"
#include "mmm_drawing.h"
#include "MMM.h"
#include <allegro.h>

In Aux, mmm is declared as:

/*INCLUDES*/
#include <allegro.h>
/*THINGS*/
BITMAP *buffy;
DATAFILE *mmm;

then set in a function in Aux, game_setup(), as

void game_setup(){
  srand(time(NULL));
  buffy = create_bitmap(SCREEN_W, SCREEN_H);
  mmm = load_datafile("MMM.dat");
}

But, when I use this function in Drawing

void draw_cursor(float w_range, int d_x, int d_y){
  float c2 = (d_x*d_x)+(d_y*d_y);
  float distance = sqrt(c2);
  if(distance < w_range)
    masked_blit((BITMAP*)mmm[crosshair].dat, buffy, 0, 0, mouse_x-16, mouse_y-16, 32, 32);
  else
    masked_blit((BITMAP*)mmm[crosshair].dat, buffy, 32, 0, mouse_x-16, mouse_y-16, 32, 32);
}

It says:
error C2065: 'crosshair' undeclared identifier
error C2228: left of '.dat' must have class/struct/union

What am I doing wrong?

HoHo

Where do you define the "crosshair" variable? I can't see it anywhere and so does the compiler
Use ->dat instead of .dat, mmm is a pointer.

SkaxCo

crosshair is a bitmap in mmm.dat. This code worked if I put it in the main body of code in main.cpp.

HoHo

Assuming that this isa preprocessor define created by dat utility you need to get the definitions in the .h that came with the .dat into the .cpp file that does the drawing. I think it is called MMM.h in your case but I might be wrong.

If it is just some random global you created yourself you need to learn extern variables. Long story short: don't define variables in headers, define them in sources and have extern versions of them in other sources/headers

SkaxCo

Well, I was forgetting to include MMM.h.

I'm going to try to decipher what you said and use it.

HoHo

Extern variables are actually relatively simple, just google for them or search the forum, there was some discussion about it some time ago. If anything is unclear feel free to come back and ask guidance

SkaxCo

Wait...so, in Drawing, in game_setup(), I would do something like

extern DATAFILE *mmm;
mmm = load_datafile("MMM.dat");

?

Because that didn't work.

Kibiz0r

In your header, you want
extern DATAFILE *mmm; //declaration
and in your source file, you want
DATAFILE *mmm = load_datafile("MMM.dat"); //definition

You have to have one formal definition of it like that, because extern just says "Yes, mmm exists somewhere, you just have to go find it" and the definition says "Here I am!".

Make sense?

SkaxCo

So I redefine it in each file? Ok, I got it, and it works. But...unrelated...my computer sucks so much, my parents are too cheap to improve it, and I can't get a job. They only got a new cooling system when it was overheating so much it wouldn't boot, and a new video card when some games I bought wouldn't run. I have like a 1.2 GHz processor and 512 of crappy RAM.

Kibiz0r
Quote:

So I redefine it in each file?

No, only define it once, in the .c/.cpp file you want to define the value in.

Any file you want to use it in, it has to be declared, using extern. You will probably accomplish this by including a header, though, to save time declaring it all over the place.

Onewing

You are talking to a computer. You must speaketh their language, but if you want a rough, un-technical translation, the word "extern" is bascially saying "I promise to declare this in my cpp file, honestly, so if anyone wants to know, it's in the cpp...honestly."

Don't break promises to the computer, they get angry.

Thread #591440. Printed from Allegro.cc