Function basics

This is a discussion on Function basics within the C++ Programming forums, part of the General Programming Boards category; Okay, I'm writing a game but that's not important, what is is that I have several large functions for basic ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    1

    Unhappy Function basics

    Okay, I'm writing a game but that's not important, what is is that I have several large functions for basic operations, I would like for these to be placed in a separate file however, they use global variables. If I place them in a new file they won't compile b/c of no access to these global variables. How do I do this, is it headers? Is there a way to link files differently. BTW, I'm using Visual C++ 6 STD.

  2. #2
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    Declare them again in the seperate file but add "extern" before them. That way the compiler can see the names declared, but will not allocate storage for them allowing the linker to fit it all together later.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    Similar problem

    I'm having a similar problem. I use several files for my functions. One "main-file" that includes the rest of the files plus some of the standard libraries (ie. stdlib.h, conio.h...).

    My problem is that some of the definitions and functions in these libraries are not accessible in the other files I include (like NULL and memset() ), only in my "main-file".

    What do I do to solve this? Something like #include extern <stdlib.h>??? (I've tried that, doesn't work ).

    I'm using Borland 5.0 (DOS compiler)
    Help someone?
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    make a header file and include it where ever you need those variables....

    put the stuff that you want to share in a header file for example

    Sharring.h or Sharring.hpp

    and wherever you need it type

    #include "Sharring.hpp"

    make sure the compiler will know where the file is though, don't hind it anywhere....

    good luck....

    Regards,
    matheo917

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    412
    You can also just #include <stdlib.h> in as many files as need functions defined in that header. All premade header files can be included as many times, in as many files, as you feel like.

  6. #6
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    Oh

    Ok, so I include "everything" in every file. Doesn't that be a waste of memory? Isn't there a way to make these included files (functions etc...) global in the entire program (in every part, every file)???

    If not: Thanks for your help!
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  7. #7
    Registered User
    Join Date
    Sep 2001
    Posts
    412
    It doesn't waste any memory. Any actual code and declarations only happens once, and the libraries are only linked once.

    The only thing it could *possibly* increase the size of would be the source code (by a few lines) and the precompiled headers.

    You don't need to include every #include in every source file, but you should include each one that you need for that file.

  8. #8
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    Ehh... help, again?

    These are the files in my project:

    File.cpp (The main file, doesn't do much... yet)
    File.h (Includes some standard libraries and declares the classes. File.h is included in every other file)
    Globals.cpp (Declares the member functions, Creates objects out of classes)
    ???.cpp (All other files...)

    These are simplified versions of my files:

    FILE.CPP
    Code:
    
    #include "File.h"
    
    #include "Globals.cpp"
    #include "???.cpp"
    #include "???.cpp"
    .... (and so on)
    
    int main()
    {
      //The actual program, not important what it does...
      return 0;
    }
    
    FILE.H
    Code:
    
    #include <conio.h>
    #include <stdio.h>
    .... (and so on)
    
    class PLAYER;
    class ENEMY;
    .... (and so on)
    
    GLOBALS.CPP
    Code:
    
    #include "File.h"
    
    //Player class
    class PLAYER
    {
      public:
        PLAYER();
        ~PLAYER();
      private:
        int Hp;
        int Mp;
        .... (and so on)
    };
    
    //Enemy class
    class ENEMY
    {
      public:
        ENEMY();
        ~ENEMY();
      private:
        int Hp;
        int Mp;
        .... (and so on)
    };
    
    //Creates the objects
    PLAYER Player;
    ENEMY Enemy;
    
    The problem is that I get the error:
    'PLAYER::PLAYER()' is not a member of 'PLAYER'

    I tried to put the member declarations in Globals.cpp into File.h like this:

    FILE.H
    Code:
    
    #include <conio.h>
    #include <stdio.h>
    .... (and so on)
    
    class PLAYER;
    class ENEMY;
    .... (and so on)
    
    class PLAYER
    {
      public:
        PLAYER();
        ~PLAYER();
      private:
        int Hp;
        int Mp;
        .... (and so on)
    };
    
    class ENEMY
    {
      public:
        ENEMY();
        ~ENEMY();
      private:
        int Hp;
        int Mp;
        .... (and so on)
    };
    
    //Creates the objects
    PLAYER Player;
    ENEMY Enemy;
    
    But then I get the errors:
    Multiple declaration for 'PLAYER'
    Earlier declaration of 'PLAYER'


    The last two errors are obvious since File.h is included several times, which means PLAYER is declared several times...

    Help please. What is wrong here?
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  9. #9
    Registered User
    Join Date
    Sep 2001
    Posts
    412
    Make all your headers like this:

    Code:
    #ifndef _THEHEADERNAME_H
    #define _THEHEADERNAME_H
    
    // _THEHEADERNAME must be unique for every header
    //put all the header stuff here.
    
    #endif
    This will make sure each header only gets included once -- so multiple inclusions won't happen. Basically, if the header has already been included, then the if statement is false, and the header won't be included again. If the header hasn't been included, the if statement is true, and the header is included.

    Also, don't use the #include for source files like .cpp files -- set them to be linked as part of the project, but don't include them in another .cpp file -- this will cause problems. #includes should be reserved for .h files.
    Last edited by The V.; 11-15-2001 at 01:28 PM.

  10. #10
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    Question Arghh


    Info :Transferring to C:\PROGRAM\BC5\BIN\tlink.exe @C:\Dokument\Cpp\Projekt\Survivor\Survivor.r$p
    Error:(1,1):_OldIRQ defined in module player.cpp is duplicated in module enemy.cpp
    Error:(1,1):_Screen defined in module player.cpp is duplicated in module enemy.cpp
    Error:(1,1):_OldIRQ defined in module player.cpp is duplicated in module object.cpp
    Error:(1,1):_Screen defined in module player.cpp is duplicated in module object.cpp
    Error:(1,1):_OldIRQ defined in module player.cpp is duplicated in module buffer.cpp
    Error:(1,1):_Screen defined in module player.cpp is duplicated in module buffer.cpp
    Error:(1,1):_OldIRQ defined in module player.cpp is duplicated in module keyboard.cpp
    Error:(1,1):_Screen defined in module player.cpp is duplicated in module keyboard.cpp
    Error:(1,1):_OldIRQ defined in module player.cpp is duplicated in module graphics.cpp
    Error:(1,1):_Screen defined in module player.cpp is duplicated in module graphics.cpp
    Error:(1,1):_OldIRQ defined in module player.cpp is duplicated in module sblaster.cpp
    Error:(1,1):_Screen defined in module player.cpp is duplicated in module sblaster.cpp
    Error:(1,1):_OldIRQ defined in module player.cpp is duplicated in module world.cpp
    Error:(1,1):_Screen defined in module player.cpp is duplicated in module world.cpp
    Error:(1,1):_OldIRQ defined in module player.cpp is duplicated in module survivor.cpp
    Error:(1,1):_Screen defined in module player.cpp is duplicated in module survivor.cpp
    Error:(1,1):Undefined symbol _Buffer1 in module player.cpp
    Error:(1,1):Undefined symbol _dataChunk in module sblaster.cpp
    Error:(1,1):Undefined symbol _fmtChunk in module sblaster.cpp
    Error:(1,1):Undefined symbol _RIFFChunk in module sblaster.cpp
    Error:(1,1):Undefined symbol _SBp in module sblaster.cpp
    Error:(1,1):Undefined symbol _Player in module world.cpp
    Error:(1,1):Undefined symbol _Object in module world.cpp
    Error:(1,1):Undefined symbol _Enemy in module world.cpp
    Error:(1,1):Undefined symbol _Keyboard in module world.cpp
    Error:(1,1):Undefined symbol _Sound in module world.cpp
    Error:(1,1):Undefined symbol _Buffer2 in module world.cpp
    Error:(1,1):Undefined symbol _World in module survivor.cpp


    Do you have any idea what might cause these errors? I did what the V suggested (the #ifndef - #define story...) in the header file. I also put the global objects there (as extern) so they were included in every file, otherwise I got the error undefined symbol blah blah...!
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  11. #11
    Registered User
    Join Date
    Sep 2001
    Posts
    412

    Re: Arghh

    Originally posted by Magos

    Info :Transferring to C:\PROGRAM\BC5\BIN\tlink.exe @C:\Dokument\Cpp\Projekt\Survivor\Survivor.r$p
    Error:(1,1):_OldIRQ defined in module player.cpp is duplicated in module enemy.cpp
    Error:(1,1):_Screen defined in module player.cpp is duplicated in module enemy.cpp
    Error:(1,1):_OldIRQ defined in module player.cpp is duplicated in module object.cpp
    Error:(1,1):_Screen defined in module player.cpp is duplicated in module object.cpp
    Error:(1,1):_OldIRQ defined in module player.cpp is duplicated in module buffer.cpp
    Error:(1,1):_Screen defined in module player.cpp is duplicated in module buffer.cpp
    Error:(1,1):_OldIRQ defined in module player.cpp is duplicated in module keyboard.cpp
    Error:(1,1):_Screen defined in module player.cpp is duplicated in module keyboard.cpp
    Error:(1,1):_OldIRQ defined in module player.cpp is duplicated in module graphics.cpp
    Error:(1,1):_Screen defined in module player.cpp is duplicated in module graphics.cpp
    Error:(1,1):_OldIRQ defined in module player.cpp is duplicated in module sblaster.cpp
    Error:(1,1):_Screen defined in module player.cpp is duplicated in module sblaster.cpp
    Error:(1,1):_OldIRQ defined in module player.cpp is duplicated in module world.cpp
    Error:(1,1):_Screen defined in module player.cpp is duplicated in module world.cpp
    Error:(1,1):_OldIRQ defined in module player.cpp is duplicated in module survivor.cpp
    Error:(1,1):_Screen defined in module player.cpp is duplicated in module survivor.cpp
    Error:(1,1):Undefined symbol _Buffer1 in module player.cpp
    Error:(1,1):Undefined symbol _dataChunk in module sblaster.cpp
    Error:(1,1):Undefined symbol _fmtChunk in module sblaster.cpp
    Error:(1,1):Undefined symbol _RIFFChunk in module sblaster.cpp
    Error:(1,1):Undefined symbol _SBp in module sblaster.cpp
    Error:(1,1):Undefined symbol _Player in module world.cpp
    Error:(1,1):Undefined symbol _Object in module world.cpp
    Error:(1,1):Undefined symbol _Enemy in module world.cpp
    Error:(1,1):Undefined symbol _Keyboard in module world.cpp
    Error:(1,1):Undefined symbol _Sound in module world.cpp
    Error:(1,1):Undefined symbol _Buffer2 in module world.cpp
    Error:(1,1):Undefined symbol _World in module survivor.cpp


    Do you have any idea what might cause these errors? I did what the V suggested (the #ifndef - #define story...) in the header file. I also put the global objects there (as extern) so they were included in every file, otherwise I got the error undefined symbol blah blah...!
    OK, let's pick just one error, and go from there.

    In which file do you define OldIRQ?

  12. #12
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    reply

    OldIRQ as well as the global objects (Player, Enemy, Object, World, Screen, Buffer1, Buffer2, Keyboard, Sound and SBp) are defined in Globals.cpp, which is included (only) in Survivor.h (the main file, the one I called File.h before).

    Survivor.h is included in all files (except Globals.cpp).

    In the error messages, they refer to objects (symbols) with an "_" in front of them. I have no such objects. Is it only the compiler (linker) that prints them that way, or does it really think such objects exists in my program?
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  13. #13
    Registered User
    Join Date
    Sep 2001
    Posts
    412
    Your problem -- never use an #include to include a .cpp file, ever.

    Do it like this:

    globals.cpp has the actual declarations of the globals.

    survivor.h should be included in all files except globals.cpp.

    In survivor.h, put the following for each global:

    extern OLDIRQ OldIRQ;
    extern PLAYER Player;

    etc.

    Your problem is that you are (indirectly) including a .cpp file in multiple other .cpp files. So the code in that file ends up being compiled multiple times, and the linker doesn't like it.

    Bottom line: never do an #include filename.cpp -- only #include filename.h. Source files are not included, they are linked; headers are included.

  14. #14
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    Unhappy Nope

    Nope! I did what you said, but the same errors showed up again.

    I have attached the files if you (or someone else) want to have a look. Perhaps you can find the error easier. The actual code is poorly commented, but since I believe it's the overall structuration that is wrong, that shouldn't matter.

    Survivor.cpp is the main file. I used the 16bit compiler in Borland to compile this (large memory model).
    Attached Files Attached Files
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  15. #15
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    Someone?

    Someone?
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 07:54 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  5. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21