Sign in to follow this  

how to pass global struct into my class function?

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi everybody,

 

I have a global struct DrawWorld with some of my variables that I need to pass to one of my function in my map class..

 

I have tried to add this to my map class:

 

 

map.h:

 

void generateMap2(DrawWorld drawWorld);

 

map.cpp: 

 

void map::generateMap2(DrawWorld drawWorld)
{ 

when I try to compile I get error:

error C2061: syntax error : identifier 'DrawWorld'

 

my map class have no idea about the existence of my struct, normally I would just #include .h file but there is not .h file as this is my main .cpp file

where the struct is defined and there is no .h file only .cpp

is there any way how I can pass this struck to my class function?

 

thanks.

Share this post


Link to post
Share on other sites

Thanks Servant,

creating a DrawWorld.h and putting my struct there and then including this to my class and main cpp as in your post worked :)

 

btw what do you mean by the inclusion guards ( #ifdef BLAH_H / #pragma once)? I have no idea what those are/do but the code seems to be working fine without them..

 

also I'm aware that having a global variables is considered bad, thats why I have the struct in the first place and planing to move it somewhere inside my main later on... (hopefully it will not break my program completely)

Share this post


Link to post
Share on other sites

Thanks Servant,

creating a DrawWorld.h and putting my struct there and then including this to my class and main cpp as in your post worked smile.png

 

btw what do you mean by the inclusion guards ( #ifdef BLAH_H / #pragma once)? I have no idea what those are/do but the code seems to be working fine without them..

 

also I'm aware that having a global variables is considered bad, thats why I have the struct in the first place and planing to move it somewhere inside my main later on... (hopefully it will not break my program completely)

The inclusion gaurds prevent the header file from being added to the code twice. It basically tells the code, "if this is already defined ignore it" that way you don't end up with duplications of the same code in your end executable once the whole thing is compiled.

 

structs aren't variables they are like classes. If you had a struct called drawWorld and you declared "struct drawWorld world;" globally, that would be a global variable.

Edited by d0ct0rscarecr0w

Share this post


Link to post
Share on other sites


structs aren't variables they are like classes without functions.

Emphasis mine.

 

Structs can have functions, just the same way classes can.

The difference between classes and structs is that classes default to private for members, while structs default to public. This also applies to inherited objects.

Share this post


Link to post
Share on other sites

 


structs aren't variables they are like classes without functions.

Emphasis mine.

 

Structs can have functions, just the same way classes can.

The difference between classes and structs is that classes default to private for members, while structs default to public. This also applies to inherited objects.

 

my mistake I will edit that part out.

Share this post


Link to post
Share on other sites

structs and classes are virtually identical. Both can contain functions and variables (and both can use inheritance).
The difference between them is that structs are 'public' by default (but can have private members), and classes are 'private' by default (but can have public members).
 
 

btw what do you mean by the inclusion guards ( #ifdef BLAH_H / #pragma once)? I have no idea what those are/do but the code seems to be working fine without them..

 

It's working fine for the moment, but can unexpectedly mess up later when you add more files to your project. Here's why:

C++ compiles files in multiple steps. At a basic level, most C++ compilers compile your code like this:
 
For every .cpp file, pre-process the text in the .cpp files to resolve any macros and defines and includes, then convert the pre-processed text to abstract code. Once all the .cpp files have been compiled to abstract code, combine and compile all the abstract code into binary/assembly/executable files.
 
There are two things to notice here:
1) C++ only compiles the source files (not the headers)
2) C++ compiles each source file independently of each other.
 
Because C++ compilers compile each source file independently, if we declare "struct World" in one .cpp file, other .cpp files don't know anything about "struct World" and give compiler errors because "it doesn't exist" as far as that file's compilation can see.
 
Oops. So what now? Every file that wants to use 'struct World' must declare it. We don't want to have to manually re-write the details of struct World over and over. So instead, we put it into a header file.
 
Header files aren't compiled directly. Compilers don't really know about header files.
 
When you say #include, what you're really saying is, "copy and paste the header file into this location before compiling".
So we write the details of struct World in the header file, and make any interested .cpp file #include struct World's header file.
 
But now we run into a different problem. Imagine we have two more headers, that both use World.
Player.h includes World.h
Monster.h includes World.h
and main.cpp includes both Player.h and Monster.h
 
When main.cpp is compiled, Player.h will be copied into main.cpp before being turned into assembly, and World.h was already copied into Player.h. So main.cpp has World.h's code copied into it.
But main.cpp also #includes Monster.h, and Monster.h also includes World.h.
So World.h's code gets copied twice into main.cpp.
 
When you do something like this in your code:

struct World
{
    //Stuff...
};
 
//More stuff...
 
struct World //AGAIN!
{
    //Stuff...
};

 
...you get error messages; you can't declare the same struct twice!
 
To prevent this from happening, we use inclusion guards.
We wrap the content of World.h so that it only gets copies once for every .cpp that (indirectly) includes it.
 
Using the preprocessor language that runs before the code is turned into assembly, we say:

#ifndef NAME     /If Not Defined: NAME
#define NAME    //Define: NAME
 
//...header content goes here...
 
#endif NAME  //End our preprocessor if-statement

 
Because NAME has to be a unique name in your project (and unique from any #defines in any 3rd-party header file your project includes), it's common practice to make NAME be in-all-caps the name of your header file, followed by _H and prefixed with a prefix unique to your project.
 
Something like this:

#ifdef PROJECTNAME_HEADERNAME_H
#define PROJECTNAME_HEADERNAME_H
 
//...content inside Headername.h
 
#endif

 
Alternatively, instead of all that #ifdef/#define/#endif stuff, you can just simply write "#pragma once" at the top of every one of your header files.
 
#pragma is for compiler-specific commands, and 'once' is the command being passed to the compiler.
 
#pragma not actually an standard C++ feature - it's a specific thing compilers can choose to implement - but almost every major modern compiler does implement it, so you're almost certainly on safe ground.
 
I personally continue to use #ifdef / #define / #endif in my projects, but I feel increasingly silly for doing so. laugh.png

Edited by Servant of the Lord

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this