Advertisement

Enough is Enough

Started by July 11, 2000 04:41 PM
6 comments, last by Indy 24 years, 5 months ago
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
Indy
in your .h file...

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


int your .cpp file...

#include "my.h"

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

void Foo() {
// code here
}



Advertisement
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
"Innocent is just a nice way to say ignorant, and stupidity is ignorance with its clothes off."words of,skitzo_smurf
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.

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-
LOL, I love the way my code gets formatted.

-BacksideSnap-
Advertisement
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.
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 <img src="wink.gif" width=15 height=15 align=middle>        


...

-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
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara

This topic is closed to new replies.

Advertisement