Archived

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

Enough is Enough

This topic is 6367 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

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 this post


Link to post
Share on other sites
in your .h file...

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


int your .cpp file...

#include "my.h"

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

void Foo() {
// code here
}



Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 <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

Share this post


Link to post
Share on other sites