Skip to main content
GameDev.net gamedev.net
🔒 Locked

Global Variables

Started by DorRaba May 4, 2004 at 11:01 AM 12 replies 3.6k views
Original Post
DorRaba
DorRaba
Hi, I''m building a project in visual c++ .NET, and I need to declare variables, typedefsn enums and constants that will be available for all of the projects files, I want to create a specific file for these things, does anyone know how to do it? another question is what is the Form1.cpp used for?
DDDDor RRRRaba
Kafeen
Kafeen
Your typedefs and enums should go in a header file included in the .cpp files.

ex :
#include "typedefs.h"

Global variables should go in a .cpp file and be externed in a header file that''s included.

ex:
extern int iVar;

Form1.cpp is used to handle the control for a MFC form. You''ll handle all the button presses and whatever else is on your MFC app from there.
DorRaba
DorRaba
I wrote this in the GlobalDef.cpp file:


#include "stdafx.h"
#include "GlobalDef.h"

extern int iVar;


and in the GlobalDef.h file, I didn't wrote anything

I included the GlobalDef.h file in the Form1.h file and I cant use the iVar variable int the Form1.h file

what should I do?

and also if I try to Declare a String object I get an error:

d:\Tzipi Project\Printing 1.01\Printing 1.01\GlobalDef.h(4): error C3145: 'tempFrm' : cannot declare a global or static managed type object or a __gc pointer

What should I do about this either?



[edited by - DorRaba on May 5, 2004 10:14:24 AM]
DDDDor RRRRaba
DorRaba
DorRaba
Please Help me, its urgent...
DDDDor RRRRaba
ray_intellect
ray_intellect
Hi,

listing ...

globals.h


extern int X;
extern int Y;



globals.cpp


#include "globals.h"

int X;
int Y;



somefile.cpp

#include "globals.h"

void someFunction(int w, int p) {
X = w; // ok because they are externs

Y = p;

}

George2
George2
globals.cpp :


int foo_dot_bar;


globals.h :

#ifndef GLOBALS_HH
#define GLOBALS_HH

extern int foo_dot_bar;

#endif


Everywhere you wanna use foo_dot_bar, you include globals.h


[edited by - George2 on May 5, 2004 10:46:15 AM]
"THE INFORMATION CONTAINED IN THIS REPORT IS CLASSIFIED; DO NOT GO TO FOX NEWS TO READ OR OBTAIN A COPY." , the pentagon
ray_intellect
ray_intellect
Hi George2,

I agree, the extern keyword means that the variable is defined externally. One question :

How does the compiler know to use the globals.cpp file?

Do you just add it to the project or is it linked with include statements?

[edited by - ray_intellect on May 5, 2004 10:51:56 AM]
DorRaba
DorRaba
Thanks, it works just fine with variables like int, bool and all of that, but when I try to to this for String* of a Form* object, I get this error message:

d:\Tzipi Project\Printing 1.01\Printing 1.01\GlobalDef.h(4): error C3145: ''tempFrm'' : cannot declare a global or static managed type object or a __gc pointer

so what can I do with that?
don''t forget that I''m using a visual c++ .NET environemnt.
DDDDor RRRRaba
Indeterminatus
Indeterminatus
ray_intellect, the global.cpp has to be part of the project in order to be compiled (it won''t be linked by includes).

the compiler doesn''t care about where the variable declared with extern is ... that''s something the linker cares about.


Indeterminatus

--si tacuisses, philosophus mansisses--
Indeterminatus--si tacuisses, philosophus mansisses--
DorRaba
DorRaba
Can anyone answer my question above?
DDDDor RRRRaba
DorRaba
DorRaba
Please help me with the question above, its urgent...
DDDDor RRRRaba
DorRaba
DorRaba
Isn''t there a way to pass this, and declare global __gc class object?
DDDDor RRRRaba

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.