#### Archived

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

# Enough is Enough

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

## Recommended Posts

This is very embrassing question for me, as I am a programmer. But how the hell do I separate my enormous program into smaller modules. I have managed to separate all my class, int''s, and my char''s, but when I try my functions I get errors saying that all my declarations are already defined. The "main.h" has all the declarations. And each of my modules i''m trying to #include "main.h" What is wrong?? I''m using VC++ 5.0 Indy

##### Share on other sites

class CWhatever {
blah;
};
void Foo();

#include "my.h"

CWhatever::Etc() {
// code here
}

void Foo() {
// code here
}

##### Share on other sites
could you give me something specific to look at? I''m not
quite sure what you are doing and could probably help if you
gave me a specific example.

skitzo_smurf

##### Share on other sites
Hey there. The problem is that you may be including the header file more than one time when everything compiles. For example one source file includes main.h, and then that header file is included in another file. This file now has main.h included twice. This will mean that the definitions will be included twice and the compiler will gripe.

To avoid this just do a #define at the top of all your headers.

For example:

#ifndef MAIN_H
#define MAIN_H

// prototype and class definitions here

#endif

Then you can include this file as many times as you want and the compiler will never duplicate it twice in a file. If it has already been included, the compiler will ignore it.

##### Share on other sites
Hi all. His problem is with functions, not classes. To answer your question NEVER EVER EVER EVER put function definitions in header files. You will get major linker errors if that header is included in more than one other source file. Instead you put the function declaration (or prototype) in the header file and the definition in a .cpp or .c file like this:

file.h:
  int function (int);[/source]file.cpp:[source]int function (int i){ i = 10; return i; // this is dumb, I know}

I''m sure this problem was pretty frustrating, I know it was for me when I ran into it way back when.

-BacksideSnap-

##### Share on other sites
LOL, I love the way my code gets formatted.

-BacksideSnap-

##### Share on other sites
Since you''re using Microsoft VC, you can also use "#pragma once" at the top of your header to ensure that it''s only included once in your build. The "#pragma" technique is also compiles a wee bit faster than the "#ifndef" technique, since it doesn''t bother to open and parse each header each time it''s included.

##### Share on other sites
this is the #ifndef ^^ mentioned...

  //////////////Player.cpp#ifndef __PLAYER_CPP#define __PLAYER_CPP//Code goes here#endif//feel free to include this file multiple times from the same .cpp file

...

-BacksideSnap- I particularily like the way it formats comments that contain keywords and smiley's
Hey, dynamic_cast mutable & volatile get colored now

Edited by - Magmai Kai Holmlor on July 11, 2000 11:34:56 PM

Edited by - Magmai Kai Holmlor on July 11, 2000 11:35:52 PM

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

• 14
• 30
• 13
• 11
• 11
• ### Forum Statistics

• Total Topics
631781
• Total Posts
3002314
×