Results 1 to 13 of 13

Thread: Class heirchy within includes problem

  1. #1

    Thread Starter
    PowerPoster Halsafar's Avatar
    Join Date
    Jun 2004
    Location
    Saskatoon, SK
    Posts
    2,339

    Class heirchy within includes problem

    Alright, I am understanding of class's and how you can host, parent, child, etc...all those nice given terms for the situations when one class needs another.

    Well I have a problem with this structure. See all my class's are in seperate header/source files...

    The only information I can find on class's that use parenting and such, define for example 4 different class's within one header, and then I assume write 1 source file for all the function to..maybe 4 source files 1 for each class.

    Given that structure I can see how it is easy to go friend this class, and declare a class with public otherclass to use the otherclass public members...all very easy.

    But I wish to keep my class's in their seperate header/source files, and yet I need a few class's to be able to borrow functions from a class within a class...I can't get it, does someone want to explain.

    I have GameEngine class, which includes my shapes engine header, so i can create an instance of Shapes....Now my SkyBox class needs functions from within Shape class, but I cannot include the shape header cuz I get a redefinition error. I cannot create an instance of the class Shape because it doesn't understand it.......I'm at a loss.

    I also had this problem when my FileHandler class needed two structs from within my GameEngine class...I couldn't find a way to get the FileHandler class to read the 2 public structs within GameEngine...so I just got rid of the class and copied all the functions into the GameEngine class....still wish I could make my FileHandler class work...damnit
    "From what was there, and was meant to be, but not of that was faded away." - - Steve Damm

    "The polar opposite of nothingness is existance. When existance calls apon nothingness it shall return to nothingness." - - Steve Damm

    "When you do things right, people won't be sure if you did anything at all." - - God from Futurama

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    You can include one header from the other. You can use pre-declarations, which simply look like
    class classname;
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3

    Thread Starter
    PowerPoster Halsafar's Avatar
    Join Date
    Jun 2004
    Location
    Saskatoon, SK
    Posts
    2,339
    So like a function declaration...

    Alright, wish to give me a bleak example
    PHP Code:
    //one header file
    #include "clsShapes.hpp"
    #include "clsSky.hpp"
    class GameEngine
    {
       
    ShapeEngine Shapes;
    };


    //another header file - clsSky.hpp
    class Sky
    {
       
    ShapeEngine Shapes;
    }; 
    How would I modify this to allow the class sky to compile.
    "From what was there, and was meant to be, but not of that was faded away." - - Steve Damm

    "The polar opposite of nothingness is existance. When existance calls apon nothingness it shall return to nothingness." - - Steve Damm

    "When you do things right, people won't be sure if you did anything at all." - - God from Futurama

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Put the clsShape include into clsSky, too.

    Make sure all your headers have multiple-inclusion-guards. They look like this:
    At the head of the file you have:
    #ifndef VERY_UNIQUE_SYMBOL_NAME
    #define VERY_UNIQUE_SYMBOL_NAME

    And at the bottom:
    #endif


    EDITED!!!!
    Because I saw a serious error in my code.
    Last edited by CornedBee; Sep 10th, 2004 at 07:58 AM.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  5. #5
    Not NoteMe SLH's Avatar
    Join Date
    Mar 2002
    Location
    192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
    Posts
    3,051
    Originally posted by CornedBee
    They look like this:
    At the head of the file you have:
    #ifdef VERY_UNIQUE_SYMBOL_NAME
    #define VERY_UNIQUE_SYMBOL_NAME

    And at the bottom:
    #endif
    I believe you could also do (at the top of the header to include only once #pragma once as an alternative to that.
    Quotes:
    "I am getting better then you guys.." NoteMe, on his leet english skills.
    "And I am going to meat her again later on tonight." NoteMe
    "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
    "my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
    Have I helped you? Please Rate my posts.


  6. #6
    Fanatic Member twanvl's Avatar
    Join Date
    Dec 2001
    Posts
    771
    #pragma once only works in MSVC++, if you really on it your code will not compile with any other compiler

  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Yes, but only in VC++.

    The MFC wizard generates this useless code:

    Code:
    #ifndef _SYMBOL_WITH_GUID_
    #define _SYMBOL_WITH_GUID_
    
    #if MSC_VER > 0x1200
    #pragma once
    #endif
    
    ...
    
    #endif
    This is absolutely idiotic. First, it uses a guard that works everywhere, then it checks if the other guard will work (i.e. it's the MS compiler in a sufficient version) and uses it if so.
    Why use the other guard in the first place, then?????
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  8. #8
    Fanatic Member twanvl's Avatar
    Join Date
    Dec 2001
    Posts
    771
    #pragma once has the (small) advantage that the preprocessor doesn't have to open the file again, it maintains a list of all files included.

  9. #9
    Not NoteMe SLH's Avatar
    Join Date
    Mar 2002
    Location
    192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
    Posts
    3,051
    Thanks for the heads up.

    I thought there might be a reason why people don't use it often .
    Quotes:
    "I am getting better then you guys.." NoteMe, on his leet english skills.
    "And I am going to meat her again later on tonight." NoteMe
    "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
    "my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
    Have I helped you? Please Rate my posts.


  10. #10
    Addicted Member
    Join Date
    Apr 2003
    Posts
    170
    Originally posted by Halsafar
    So like a function declaration...

    Alright, wish to give me a bleak example
    PHP Code:
    //one header file
    #include "clsShapes.hpp"
    #include "clsSky.hpp"
    class GameEngine
    {
       
    ShapeEngine Shapes;
    };


    //another header file - clsSky.hpp
    class Sky
    {
       
    ShapeEngine Shapes;
    }; 
    How would I modify this to allow the class sky to compile.
    how about:
    PHP Code:
    //another header file - clsSky.hpp
    #include "clsGameEngine.hpp"
    class Sky
    {
       
    ShapeEngine Shapes;
    }; 
    and dont forget that either "clsGameEngine.cpp", or "clsGameEngine.lib" (the compiled version), should be available.
    well, just as cornedbee said

  11. #11

    Thread Starter
    PowerPoster Halsafar's Avatar
    Join Date
    Jun 2004
    Location
    Saskatoon, SK
    Posts
    2,339
    How do I compile my class's cpp file into a lib file?
    "From what was there, and was meant to be, but not of that was faded away." - - Steve Damm

    "The polar opposite of nothingness is existance. When existance calls apon nothingness it shall return to nothingness." - - Steve Damm

    "When you do things right, people won't be sure if you did anything at all." - - God from Futurama

  12. #12
    Addicted Member
    Join Date
    Apr 2003
    Posts
    170
    check the "debug" and "release" folders in ur project directory,
    they are full of .obj files.
    as much as i know they are similar to .lib, maybe someone else could
    do a better job explaining how to use already compiled code.
    i just know this is possible as an idea, but nvr did it actually !
    because the idea is that, the linker's job is to link one compiled code
    with the address of another function compiled in another file,
    and add them all up to an exe.

    try to mess with project->settings->link->general->object\library modules
    Last edited by ZaidGS; Sep 10th, 2004 at 11:33 PM.

  13. #13
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    If you want to create a .lib (static link library), create a static library project and put all cpps you want in there into it.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width