# Defining a struct in a header so that I can use my varaible anywhere

This topic is 1175 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm trying to define a struct within my header file to use  the new struct variable  all over my code. I'm having some difficulties doing this. Any examples please? I looked around before coming here but nothing is working. A bare bone example would be appreciated.

##### Share on other sites

Note that inline initialization of myMemberVariable requires C++11 or better. That is, if you don't have C++11 support for some reason, do this instead:

struct MyStruct
{
int myMemberVariable;
};
Edited by Josh Petrie

##### Share on other sites
//MyHeader.h

struct MyStruct
{
int myMemberVariable = 357;
};



.

#include "MyHeader.h"

int main()
{
MyStruct myStruct;
myStruct.myMemberVariable = 753;

return 0;
}

//MyHeader.h

struct MyStruct
{
int myMemberVariable = 357;
};



.

#include "MyHeader.h"

int main()
{
MyStruct myStruct;
myStruct.myMemberVariable = 753;

return 0;
}

my code looks very similar to this except I didn't define the variables declared within my struct. I keep getting the error C2236 from Microsoft visual studious 2010.... am I suppose to type "MyStruct"

Edited by LAURENT*

##### Share on other sites

I'm trying to define a struct within my header file to use  the new struct variable  all over my code. I'm having some difficulties doing this. Any examples please? I looked around before coming here but nothing is working. A bare bone example would be appreciated.

I'm ganna go another route, and assume you mean to do the following:

//MyHeader.h

struct Struct{
int x;
};
extern Struct mStruct;
#endif

//MyHeader.cpp
Struct mStruct = {0}; //note we initialize it's members to 0.

//main.cpp
#include <iostream>

int main(int argc, char **argv){
std::cout << mStruct.x << std::endl;
}

The keyword your looking for is extern, it's basically like forward-declaring a function, but for a global variable. you define it in a single implementation file(in this case MyHeader.cpp), and when linking is done the linker will find it and use it for other implementation files.

With that said, I do not advocate the use of extern, and that using it generally means something bad is going on(this is really true if you are using any global variables). as such i don't advocate it's use at all.

edit: ninja'd by frob. Edited by slicer4ever

##### Share on other sites

int myMemberVariable = 357;

will not work in VS2010. That's what Josh Petrie wrote requires a 2011 or later compiler.

##### Share on other sites

I'm not sure that's quite what he is looking for.

use  the new struct variable all over my code

A value that you can reference in many files is a global variable

They're normally a bad idea, but in the interest of saying HOW first...

In C++ there are two parts.  You declare a variable by saying "Somewhere in the executable there is an object with this name and type." You define a variable by saying "Here is an actual thing with this name and type".

The first part is to declare that the variable exists.  Usually this is done in a header file, marking the variable name as "extern".  That means "This variable is created externally, somewhere else, I'm only telling you about it."

-----------------
struct MyStruct { ... };
extern MyStruct myGlobalVariable;  // Declaring that somewhere in the program, there is an instance with this name

The second part is to define the variable somewhere.  Usually this is done in an implementation file. This actually creates the instance that everyone else can use.

somefile.cpp
------------
MyStruct myGlobalVariable; // Defining this somewhere in the program. There can be only one.

Note that this one does not have extern in front of it.  It is an actual instance.

When the files all get linked together, all the files that use the external declaration (from the header file) will try to link to the actual data.  Only one file can have the actual instance.

Be careful that there is only one of those without the extern.  If you forget the extern in the header, or if you have multiple definitions with the same name, you will get an error that the name is defined more than once.

Normally this is considered a bad pattern.

It allows information to be shared without any access controls, without any checks and balances, any part of code can change the value at any time.

It can be used for some well-known instances.  For example, you are probably used to cin and cout. These are global variables.
It can be a source of nasty evil hard bugs to hunt down, since you know that the value was set to one thing at one point, but then sometime later it is set to something different.

As an example of such a bug, imagine you are writing values with cout.  At the beginning of your program, you start writing numbers with cout. Everything works nice.  Then, midway through your program, suddenly all the numbers turn into hex values!  Unknown to you, some library you've used changed the format flag of cout to make integers display in hex format rather than decimal format.  You have no idea how or why, the global variable has just changed out from under you.

Beware of using them. Global variables are usually an anti-pattern.

Alright, I did it this way but somehow the struct named "Square" isn't recognized, and on top of that I'm still getting  the same error in the header file.  I need almost every portion of the code to be able to recognize what "Square" type variables are.

Edited by LAURENT*

##### Share on other sites

Post your actual code and your actual error message. Anything else is just going to have people guessing.

I thought I knew exactly what I needed to know because I did a bit of research.... it just I was slightly off track. Here is the code.

#ifndef GLOBAL_H_INCLUDED
#define GLOBAL_H_INCLUDED
#include "SDL.h"

//Error C2236 detected here "unexpected 'struct' 'Circle'. Did you forget a ';'?"
struct Circle
{
int x ;
int y ;
int r ;
};

struct Square
{
int x, y;
int w, h;

int front_cliff;
};

struct Triangle_Object{
// A triangle needs three points. We'll make lines using fomulas
int x1, y1;
int x2, y2;
int x3, y3;
};

//The surfaces
extern SDL_Surface *Character1; // TESTING ONLY
extern SDL_Surface *Screen ;
extern SDL_Surface *FloorTileSheet ; // Floor tile art only
extern SDL_Surface *FloatTileSheet ; // for all floating objects

//Sprite from the tile sheet
extern SDL_Rect Character_Test_Clips[ ];
extern SDL_Rect Floor_Object_Clips[ ];
extern SDL_Rect Float_Object_Clips[ ];

//The event structure
extern SDL_Event event;

//The camera
extern Square Camera ;

#endif


Edited by LAURENT*

1. 1
2. 2
3. 3
4. 4
Rutin
19
5. 5

• 14
• 14
• 9
• 9
• 9
• ### Forum Statistics

• Total Topics
632926
• Total Posts
3009252
• ### Who's Online (See full list)

There are no registered users currently online

×