#### Archived

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

# Correct way of doing structs,and functions in multiple files

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

## 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;

} 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 on other sites
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

#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 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 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 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 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 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_Hstruct GameStates_S{ unsigned char Paused; unsigned char FullScreenFlag; unsigned char Mode;//Any other crap for this example :).};extern GameStates_S GameStates;#endif

//GameSystem.cppGameState_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 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 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 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 on other sites
I also hate to correct you, but look here. Note specifically "The variable or function may be defined in another source file, or later in the same file." Another "SOURCE" file.

##### Share on other sites

What does not make sense?

But hey don't take my word for it.

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

Okay, for example, it is quite common to use in a header file

using namespace std;

so you don't have to

std::vector

understand? There is nothing wrong with using a namespace in a header file.

I don't understand who has taught you or what books you have read, but maybe you need to pick up a good one ? If you think it is bad practice to use namespaces, you can kiss stl good bye then!

EDIT:

If you want to talk about well known programmers, take a look at some of Nettles' or Carmack's code! I wonder if they ever used a namespace in a header file ?

[edited by - RhoneRanger on December 15, 2003 11:57:37 PM]

##### Share on other sites
quote:
Original post by RhoneRanger

quote:

What does not make sense?

But hey don't take my word for it.

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

Okay, for example, it is quite common to use in a header file

using namespace std;

so you don't have to

std::vector

understand? There is nothing wrong with using a namespace in a header file.

I don't understand who has taught you or what books you have read, but maybe you need to pick up a good one ? If you think it is bad practice to use namespaces, you can kiss stl good bye then!

It is quite obvious that you're not a very experienced C++ programmer so I will be easy on you.

If you do a "using namespace std;" in a header file, you do what is called "polluting the global namespace". This is because you pull all the identifiers from the std namespace out into the global one. This defeats the whole purpose of namespaces and leads to name clashes.

What you normally would is

• Either put the using directive in function scope, or
• write using std::cout; using std::endl; if that are the names you need etc.

You never ever put a using namespace std; in a header file. Period.

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:55:31 PM]

##### Share on other sites
Please stop trying to pass your ignorance on to the younger generation.

##### Share on other sites
quote:
Original post by Deyja
Please stop trying to pass your ignorance on to the younger generation.

Who was that for? Me?

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

William James (1842 - 1910)

##### Share on other sites
Yes rohde, it was for you!

##### Share on other sites
No, not you. I encourage you to pass on your ignorance. It''s the other guy I''m flaming. Rhoney.

quote:
I don''t understand who has taught you or what books you have read, but maybe you need to pick up a good one ? If you think it is bad practice to use namespaces, you can kiss stl good bye then!

That guy, who hasn''t actually looked at any of carmacks code enough to relieze how much it sucks.

##### Share on other sites
Put all your global declarations into headers and mark them extern. Put all your global definitions into translation units. Mark everything else static, except functions you want to be global. Put function declarations into headers as well but you don''t have to mark them extern. This is what the headers should look like in C:

typedef struct
{
// data
}Foo;

extern Foo myfoo; // global myfoo declaration
void Bar(Foo *pFoo); // declaration, not required but helps with debugging

Inside Foo.c file:
#include Foo.h

static int k = 5; // file local declaration and definition
Foo myfoo; // program global data
void Bar(Foo *pFoo){}// global function
static void Barf(){}// file local function

You can also put macros and enums into both headers and c-files if you want. Then include that header into anything that needs to use the structs or global functions. You can also go w/o using headers but that is lot of code duplication and tedious/error prone work.

That struct Foo is only a type and not an actual instance. You can duplicate that type by hand in all files that need it but I rather you put that into single header and include that. Basically the c-file will have all the functions that operate on Foo. That''s the idea behind modular programming. OO is basically the same thing meaning each module has local state and instance state. C++ packages it into a class or object. Object too has class state that is the same for every instance and instance specific state plus functions.

In c++ I think definitions default to statics while in C they''re global. You don''t use static keyword in C++ but unnamed namespaces like so:

namespace {int i = 5;}

though you can use statics as well like so:

static int i = 5;

static keyword for file statics is deprecated in c++ but not for function statics data. Static is an overloaded keyword, it means both lifetime and visibility. I wish they used separate keywords instead as it''s confusing as heck.

This C++ class has all the elements of modular style.

class Foo
{
public:
void Bar();
static void Barf();
private:
void Foo();
int i;// instance variable - different for each instance of Foo
static int k;// class variable - same for all instances of Foo equivalent to file static data as in the C module
};
int Foo::k = 44;// needed definition in header file

The only common thing between OO and module is global data now. You package it with the most appropriate cpp/c file because you can''t have multiple definitions across files unless they''re marked static. You package globals with most appropriate file because you don''t want to drag other globals with it that might not be needed, might cause recompilation if defined in different c/c++ files but declared in one header. Everytime you change that global in one of those c/c++ files all files that touch that header have to be recompiled I think.

##### Share on other sites
quote:
Original post by RhoneRanger
Yes rohde, it was for you!

Okay please provide me with ONE credible source that advocates the use of using directive in a header file.

I give you:

• Herb Sutter (the author of many books and leading the C++ compiler effort at MS).
• Andrew Koenig (author of Accelerated C++ and other books and on the Standard C++ board if I am not mistaken).
• Bjarne Stroustrup (creator of C++)
• Francis Glassborow ( on the Standard C++ board if I am not mistaken, and well known C++ programmer).
• Bruce Eckel (author of many C++ books and former member of the C++ board)

So when I say credible I mean of equally stature. Just ONE (althoug I have given you 5 EXTREMELY credible sources).

The ignorance is astounding.

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 12:03:39 AM]

[edited by - rohde on December 15, 2003 12:04:03 AM]

[edited by - rohde on December 15, 2003 12:04:50 AM]

##### Share on other sites
quote:
Original post by Deyja
No, not you. I encourage you to pass on your ignorance. It''s the other guy I''m flaming. Rhoney.

quote:
I don''t understand who has taught you or what books you have read, but maybe you need to pick up a good one ? If you think it is bad practice to use namespaces, you can kiss stl good bye then!

That guy, who hasn''t actually looked at any of carmacks code enough to relieze how much it sucks.

Hmmm, a little arrogant are we?

There is nothing wrong with using a namespace in a header file. If done correctly, it organizes things very very well!

A difference of opinion is nothing to get all pissy over!

I do use namespaces quite a bit, and I am not a novice programmer. I am done arguing, just know that it works very well.

Sorry if I got pissy, but it frustrates me when someone says it is a sign of a bad programmer... bla bla bla

Don''t tell me that! I am working with a team of top notch programmers on this project. Tell me again how using namespaces in a header is bad or a sign of a bad programmer? BAH

##### Share on other sites
quote:
Original post by RhoneRanger
quote:
Original post by Deyja
No, not you. I encourage you to pass on your ignorance. It's the other guy I'm flaming. Rhoney.

quote:
I don't understand who has taught you or what books you have read, but maybe you need to pick up a good one ? If you think it is bad practice to use namespaces, you can kiss stl good bye then!

That guy, who hasn't actually looked at any of carmacks code enough to relieze how much it sucks.

Hmmm, a little arrogant are we?

There is nothing wrong with using a namespace in a header file. If done correctly, it organizes things very very well!

A difference of opinion is nothing to get all pissy over!

I do use namespaces quite a bit, and I am not a novice programmer. I am done arguing, just know that it works very well.

Sorry if I got pissy, but it frustrates me when someone says it is a sign of a bad programmer... bla bla bla

Don't tell me that! I am working with a team of top notch programmers on this project. Tell me again how using namespaces in a header is bad or a sign of a bad programmer? BAH

Well it is the truth. 90% of the times it IS a sign of a bad programmer. That of course doesn't mean that a good programmer couldn't and wouldn't do it on rare occasions, but normally it should be avoided.

Further it is clear from your posts that you don't really know what the using directive does. That's ok; just don't pull shit out of your arse.

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 12:13:02 AM]

##### Share on other sites
using the namespace keyword in a header is awesome for organizing code... Using the "using namespace" keyword in a header can lead to trouble. Especially the open "using namespace blah;" ended one.

If one must use the "using" keyword, you should at least use the { and }.

My two cents anyway

##### Share on other sites
quote:
Original post by pjcast
using the namespace keyword in a header is awesome for organizing code... Using the "using namespace" keyword in a header can lead to trouble. Especially the open "using namespace blah;" ended one.

If one must use the "using" keyword, you should at least use the { and }.

My two cents anyway

True.

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

William James (1842 - 1910)

##### Share on other sites
quote:
Original post by rohde
That''s ok; just don''t pull shit out of your arse.

What the fuck are you talking about ? Are you drunk? high perhaps? I am not pulling shit out of my arse.

Tell me again what credibility you have? BAH go have some cake and sodomy. You are a person who talks as if he knows, but has just read some papers. To you I say good day!

##### Share on other sites
quote:
Original post by RhoneRanger
quote:
Original post by rohde
That's ok; just don't pull shit out of your arse.

What the fuck are you talking about ? Are you drunk? high perhaps? I am not pulling shit out of my arse.

Tell me again what credibility you have? BAH go have some cake and sodomy. You are a person who talks as if he knows, but has just read some papers. To you I say good day!

Look dude, there's absolutely no reason to be hostile. This is all in good manner Seriously!

When you say that it is a good way to organize code by having a "using namespace std;" in your header file you are in effect pulling shit out of your arse. Sorry, but it's true.

But hey, I have given you good arguments as to why you should NOT do it, yet you have not given a single argument in favaour of it.

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 12:27:10 AM]