CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 22

Threaded View

  1. #1
    Join Date
    Aug 2011
    Posts
    13

    What is wrong with my code organization?

    First off, let me introduce you to the method of code organization I've been using. I'll have one main .cpp file where the basic functionality of the entire program can be understood. High-level functions called by the main .cpp file are placed in a .h file. Low-level functions called by the high-level functions are placed in other .h files and so on.

    No compiler problems until I get to the issue of struct/object recognition among different .h files.

    I've encountered problems with global structs being recognized in some compiled .h files, but not in others.

    for example:

    Code:
    #ifndef low_level1
    #define low_level1
    
    struct my_struct
    {
      int x;
      int y;
    } its_mine;
    
    #endif
    Now, if I have several low-level .h files that use its_mine, and I #include "low_level1" in all of these other .h files, eventually I end up with compiler errors like:

    Code:
    error C2065: 'its_mine' : undeclared identifier
    when some of the .h files are being compiled. Now I know what the error message means, it just means that the declaration of its_mine isn't found for some of the .h files even though I used #include "low_level1.h". This doesn't make sense to me why.

    I temporarily fixed this by making all my globally used structs into objects and placing them all in an objects.h.

    Now I have

    Code:
    #ifndef objects
    #define objects
    class my_class
    {
    public:
        int x;
        int y;
    };
    my_class its_mine;
    
    #endif
    I used #include "objects.h" in all of the .h files that used these objects and this worked for long enough that I thought that this was a solution to my woes. However, as my project grew I ended up with the same problem.

    This isn't a method of code organization I learned from someone, it's just one I've used to serve my needs but I don't understand why compilers (I'm using VS2008) don't like it.

    So my question is what is wrong with my code organization (besides the obvious answer everything)?

    In case this isn't a question that can be answered without an exact example (some people make it sound like no question is) here are some snippets (I really hope no one thinks it's necessary to ask for all 10+ files and 5000+ lines of code...)

    Code:
    #ifndef objects
    #define objects
    
    class Squares{
    
    int member;
    
    };
    
    #define SquaresY 13
    #define SquaresX 6
    Squares Square[SquaresY][SquaresX];
    
    #endif
    Code:
    #ifndef image
    #define image
    
    #include "objects.h"
    #include "low_level1.h"
    #include "low_level2"
    
    void myfunction()
    {
       if((Square[0][0].member != 0)
       {
       ;
       }
    }
    #endif
    sample error:
    c:\visual studio\....\image.h(33) : error C2065: 'Square': undeclared identifier

    wheras Square is recognized as declared in several other .h files:
    low_level1.h,
    low_level2.h, etc.

    and:

    Code:
    #ifndef low_level1
    #define low_level1
    
    #include "objects.h"
    #include "image.h"
    #include "low_level2.h"
    
    // Compiler recognizes Square
    // Code that uses Square....
    
    #endif
    Code:
    #ifndef low_level2
    #define low_level2
    
    #include "objects.h"
    #include "image.h"
    #include "low_level1.h"
    
    // Compiler recognizes Square
    // Code that uses Square....
    
    #endif
    Last edited by notinasia; June 12th, 2012 at 02:51 PM.

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

Featured