Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Arch_DOA

Correct way of doing structs,and functions in multiple files

This topic is 5416 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 I cant really find any good tutorials on linking files the "correct" way. I''ve been looking at the half-life source and the doom source to see how the "pros" do it I guess. So would this be the correct way for the header file?
#ifndef _M_SQUID_H
#define _M_SQUID_H

#include <iostream>

using namespace std;

struct Squid {

	int health;
	int power;
	bool isdead;

} extern squid; 

extern void Attack(); 

#endif
then if I put it in my .cpp file

#include "m_squid.h"

Squid squid;

void Attack() {

	cout <<"Attack"<<endl;
}
is this the official way of doing these things, or is there some other way. It seems wierd to do define it that way with structs though(How I defined the Squid variable). I never really saw that in either source. Is there another way to do this. Thanks for any help.

Share this post


Link to post
Share on other sites
Advertisement
Okay, you are getting close, but there are a few things you need to understand.

First off, the extern keyword is used when you want to access something from a different source file, Ill give a short example later.

In your case, here would be the correct way to do what you are doing. In the header file is the class/function decleration, and in the cpp file are the function bodies.




#ifndef _M_SQUID_H
#define _M_SQUID_H
#include <iostream>
using namespace std;
struct Squid
{
int health;
int power;
bool isdead;
void Attack();
};

#endif

and in your cpp file

#include "m_squid.h"

void Squid::Attack()
{
cout <<"Attack"<<endl;
}


Now to make Squid work in a certain file, you of course would include squid.h.

EXAMPLE, say this file is called "SquidGame.cpp"


#include "m_squid.h"


Squid Squid1;

Squid1.Attack();



And finally, here is how the extern keyword is used. Say you have another file called "Idunno.cpp", and you want to acces Squid1.

   
#include "whatever.h"

extern Squid Squid1;



now you can acces Squid 1 in Idunno.cpp. I hope this helped you understand a little more, and good luck and happy coding!

EDIT: I forgot a ; in the example code.




[edited by - RhoneRanger on December 15, 2003 8:29:55 PM]

Share this post


Link to post
Share on other sites
Ok I see what your saying. I was sortof doing it backwards. Its more of saying its defined somewhere else other than saying im defining it extern so it can be used somewhere else. Anyway I was having trouble trying to get my other header files together. Like I had a game struct and it wasnt letting me define it like Game game1; in the cpp file and putting extern Game game1; in the game.h file. Its hard to explain. So I created a "common.h" file to put all the extern definitions into. Is this a correct way to do this. Here take a look



#ifndef _GAME_H
#define _GAME_H

#include <iostream>

using namespace std;

#include "m_squid.h"
#include "player.h"

struct Game {

void Out(char* text,int line);
void Pause();
void Clear();
};

#endif



see I included all my player and squid definitions in game. then I created a common file



#ifndef _COMMON_H
#define _COMMON_H

#include "game.h"

extern Game game;
extern Squid squid;
extern Player player;


#endif



It wouldnt ever let me do this though.

#ifndef _GAME_H
#define _GAME_H

#include <iostream>

using namespace std;

#include "m_squid.h"
#include "player.h"

struct Game {

void Out(char* text,int line);
void Pause();
void Clear();
};

extern Game game;

#endif


Im not really sure why it didnt let me. Any furthur explaination on this would be fine. I had the Game game; defined in game.cpp. Anyway thanks for all the help.



Share this post


Link to post
Share on other sites
I have just one thing for now:

It is WAY too reckelss to have a using directive in a header file.



Human beings, by changing the inner attitudes of their minds, can change the outer aspects of their lives.

William James (1842 - 1910)

Share this post


Link to post
Share on other sites
yeah, shouldn''t you try and move those include''s from the header into the source instead? I think it''s better to only include the files that''s needed for the struct/class definition into the header.





--{You fight like a dairy farmer!}

Share this post


Link to post
Share on other sites
rohde, nonsense!

That is common practice.

Arch, an extern is usually in a cpp file, and it needs to be a namespace, class, variable etc defined in a different cpp file.

Don''t create "common" to use all your externs! That is not good!

Instead pass them through a function. I rarely use extern at all, unless it is for 1 specific purpose, in 1 cpp file. A better solution is passing the struct through a function, that way you can make it work for ANY struct of the same type.

like in a cpp file you may have a function like

void Attack(Player, Squid);



that way the player can attack ANY squid, not just Squid1 like in the previous example.

He could do

Attack(Player,Squid2);

or if it was multiplayer

Attack(Player2,Squid300);


Understand ?

Share this post


Link to post
Share on other sites
quote:
Original post by RhoneRanger
rohde, nonsense!

That is common practice.

Arch, an extern is usually in a cpp file, and it needs to be a namespace, class, variable etc defined in a different cpp file.

Don''t create "common" to use all your externs! That is not good!

Instead pass them through a function. I rarely use extern at all, unless it is for 1 specific purpose, in 1 cpp file. A better solution is passing the struct through a function, that way you can make it work for ANY struct of the same type.

like in a cpp file you may have a function like

void Attack(Player, Squid);



that way the player can attack ANY squid, not just Squid1 like in the previous example.

He could do

Attack(Player,Squid2);

or if it was multiplayer

Attack(Player2,Squid300);


Understand ?


Actually, i''m sorry to have to correct you, but you normally put the extern in the .h file. That way, any sources include the header can use said variables without having to write it out manually. For example:


//GameSystem.h

#ifndef GameSystem_H
#define GameSystem_H

struct GameStates_S
{
unsigned char Paused;
unsigned char FullScreenFlag;
unsigned char Mode;
//Any other crap for this example :).

};

extern GameStates_S GameStates;
#endif



//GameSystem.cpp

GameState_S GameStates;



Now, anything that includes this file has access to GameStates, rather than having to put an extern GameStates_S GameStates; line into each and every .cpp file that needs it.

Share this post


Link to post
Share on other sites
quote:
Original post by RhoneRanger
rohde, nonsense!

That is common practice.


No it''s not. It pollutes the global namespace. It''s poor practice and sign of a bad programmer.




Human beings, by changing the inner attitudes of their minds, can change the outer aspects of their lives.

William James (1842 - 1910)

Share this post


Link to post
Share on other sites
quote:
Original post by rohde

No it''s not. It pollutes the global namespace. It''s poor practice and sign of a bad programmer.




How can it possibly pollute the global namespace if you are only using it ?

You make no sense.

Share this post


Link to post
Share on other sites
quote:
Original post by RhoneRanger
quote:
Original post by rohde

No it's not. It pollutes the global namespace. It's poor practice and sign of a bad programmer.




How can it possibly pollute the global namespace if you are only using it ?

You make no sense.




What does not make sense?

Normally I don't use the using directive that often, and if I do only in a very small scope, and even the staunchest advocate of the using directive forbids its use in a header file because of the reason that _I_ might make the decision to deal with name clashes, but I shouldn't impose that decision on other clients of my code.

But hey don't take my word for it.

Here's Herb Sutter (you do know him right?): "I recommend never writing using directives or using declarations in header files (among other advice). See my article in the October 2000 issue of Dr. Dobb's Journal..."

Or Francis Glassborow: "A programmer who understood the motivation for namespaces would rarely consider placing a namespace directive declaration at file scope in a header file."

Or Bruce Eckel (author of "Thinking in C++" and former member of the Standard C++ board): "However, the using directive exposes only those names for the cureent file, so it is not quite as drastic as it sounds. (But think twice about doing it in a header file - that is reckless.)" (his own emphasis).

If you still don't understand it get a decent C++ book.



Human beings, by changing the inner attitudes of their minds, can change the outer aspects of their lives.

William James (1842 - 1910)

[edited by - rohde on December 15, 2003 11:49:16 PM]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!