#### Archived

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

# Real Programmers Could Probly Help Me

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

## Recommended Posts

OK guys, I'll tell it to ya straight: I am a completely self-taught wannabe game programmer who doesn't even know the correct way to include more than one .cpp file into his project. I'm trying to finally organize my files the right way and understand module programming. I've created this GUI system with 7 classes: GUI_OBJECT - base class GUI_WINDOW - window class (derived from GUI_OBJECT) GUI_CONTROL - control base class (derived from GUI_OBJECT) and 4 control classes derived from this I've created a header file for each class and a source file for each of the 4 controls and the window class. However, I can't figure out a way to compile them all together without double defining them and including the main headers(ie ddraw.h, stdio.h, math.h, gameheader.h) multiple times. I've tried to use preprocessor directives like:
  #if !defined(GUI_CONTROL) #define GUI_CONTROL 1 #include "gui_control.h" #endif 
but it always says 'error: base class undefined' for the control headers. Any help would be appreciated. I'm running out of options fast. pointguard1 Edited by - pointguard1 on November 23, 2001 2:29:31 AM

##### Share on other sites
the usual method is something like...

// stuff that should be in someheader.h goes here, class
// definitions etc

#endif

- in somecpp.cpp

##### Share on other sites
  #ifndef GUI_OBJECT_h#define GUI_OBJECT_hclass GUI_OBJECT{};#endif

.

  #ifndef GUI_WINDOW_h#define GUI_WINDOW_h#include "GUI_OBJECT.h"class GUI_WINDOW : public GUI_OBJECT{};#endif

Edited by - burp on November 23, 2001 2:41:55 AM

##### Share on other sites
If you are absolutely, 100% sure that you are going to compile this under MSVC or a compatible compiler with regard to this behavior, you can use #pragma once:
#pragma once   // this file will now only be included onceclass whatever{...};

It works under GCC, but you get "deprecated" warnings.

##### Share on other sites
Thanx guys!

Newbie to game programming?
visit

KaBooMgames
Great for Newbs, Masters welcome as well

##### Share on other sites
I usually go for something like this:
#ifdef MSC_VER // it might actually be _MSC_VER or something;               // - check the Windows headers to be sure#pragma once#endif#ifndef THIS_FILE_H#define THIS_FILE_H...main body of file here#endif // THIS_FILE_H

If you''re comfortable with VB, you can put all this text into an MSVC macro in 10 minutes, and just click a button to put it all into your new files.

##### Share on other sites
I'm not absolutely sure, but aren't #pragma directives ignored by other compilers if they don't support them? I typically use:

#ifndef HEADER_H#define HEADER_H#pragma once...#endif // HEADER_H

Special thanks to VisualAssist -- unlike macros (click a button), I just type /**** and it'll insert my whole header file template, and even prompt me for the #define'd text.

I've seen pointguard1's style mainly in older unix code, but I think that's what they used with golgotha as well... but I could be wrong :-)

Edit: Just checked the MSDN, which says "If the compiler finds a pragma it does not recognize, it issues a warning, but compilation continues".

Edited by - xeos on November 24, 2001 6:45:30 PM

##### Share on other sites
quote:
Original post by xeos
I''m not absolutely sure, but aren''t #pragma directives ignored by other compilers if they don''t support them?

Generally, but how are you to be sure that 2 compilers don''t inadvertently support the same #pragma directive in different ways?

The only totally safe thing to do with implementation-specific code is to wrap it in #ifdefs.

##### Share on other sites
Firstly, an answer to the question:

You must make sure that you include the GUI_BASE class''s header _before_ the derived classes. You can use:
//// THIS: Include the base class before defining the derived class -- check for cyclic includes (a->b->a->b->...)//#ifndef __DERIVED_H__#define __DERIVED_H__// Include the base class#include "base.h"...#endif//// OR: include the base class before the derived class in the CPP file.//// Derived.cpp#include "base.h"#include "derived.h"...//// OR: Use 1 header as a "Global include" file, listed in hierachical order//// GlobalInclude.h#ifndef __GLOBAL_H__#define __GLOBAL_H__// Include ALL classes#include "base.h"#include "derived.h"#endif// derived.cpp#include "GlobalInclude.h"         // includes ALL headers

quote:
Original post by Kylotan
Generally, but how are you to be sure that 2 compilers don''t inadvertently support the same #pragma directive in different ways?

The only totally safe thing to do with implementation-specific code is to wrap it in #ifdefs.

Writing code in VC++ almost guarantees it can''t be ported anyway . But seriously, which compilers use #pragma once for something else? And, using your argument, how can you be certain that a compiler, or some included header, doesn''t #define MSC_VER as something else? In order to have source which compiles with multiple compilers, or on mutliple platform, you usually need to do a great deal of configuration for each (eg XFree86: compiles for nearly every OS under the sun, and has more than enough platform config options). When you get to this level of programming, it''s probably assumed that you know what #pragmas a compiler supports .

I don''t think portability is a major concern for somebody who hasn''t learnt how to use more than one .cpp file. When I started out with VC++, I hated all the rubbish the "New Class" command made -- there was too much junk there. So I very quickly found something better (VisualAssist), and it spread through our office like a virus. But it made things simpler!

When you''re starting out, you want simplicity cause your head can''t take it all in. When you more experienced, you keep things simple because it is easier to manage and maintain. I just wish I could have had a more simple learning environment than Borland C++ (although a book or some tutorials would have helped ).

Sorry about the rant, but I''ve got a few hours to kill tonight before I go to bed... hope the info above helps though!

Simon Wilson,
XEOS Digital Development

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

• 13
• 26
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
633735
• Total Posts
3013597
×