[beginner] Include problems tidying up code
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: [beginner] Include problems tidying up code

  1. #1
    Join Date
    Dec 2010
    Posts
    2

    [beginner] Include problems tidying up code

    I've currently trying to convert a little project written in C++ into a more conventional style. I'm learning the very basics of C++ and I thought it would help me to understand. What the original author has done is write a lot of code in header files. He has used them almost exclusively. I thought it would be a good idea to create proper header and .cpp files.

    The header I'm looking at right now has no includes or anything. It goes straight into defining the one function that it consists of. When I try to put the prototype of the function in the header, and the actual function into its own .cpp file I get a lot of problems with non included variables (even namespace std).

    What was available to the old header when it had the whole function in it? How could the old header have this information and the cpp file I created doesn't?

    Just to clarify:
    older header had:
    void func_name() {
    code, no sign of any includes
    }

    Now:
    heade:
    void func_name();

    cpp file:
    void func_name() {
    code, no sign of any includes
    exact same as previous.h
    }

    Thanks you for your help.

  2. #2
    Join Date
    Nov 2007
    Posts
    35

    Re: [beginner] Include problems tidying up code

    Post some code that demonstrates the problem or I don't think anyone can figure out what the problem is.

  3. #3
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    3,763

    Re: [beginner] Include problems tidying up code

    the old header... unless it gets included somewhere... doesn't get compiled. So it can contain just about anything and not cause any problems.

  4. #4
    Join Date
    Dec 2010
    Posts
    2

    Re: [beginner] Include problems tidying up code

    It does get included. It's so frustrating working with this sloppy code. I mean I'm no good but even to me this looks like bad news. I thought there might be some logic to it but apparently not. I'll keep investigating other routes...

  5. #5
    Join Date
    Jul 2010
    Posts
    88

    Re: [beginner] Include problems tidying up code

    Remember that each cpp file must include it's own header so that the functions can call each other.
    I use these macros to keep loops clean and simple.
    #define LoopForward(min,var,max) for(var=min;var<=max;var++)
    #define LoopBackward(min,var,max) for(var=max;var>=min;var--)
    #define LoopForwardStartAndLength(var,start,length) LoopForward(start,var,((start)+(length))-1)
    #define LoopBackwardStartAndLength(var,start,length) LoopBackward(start,var,((start)+(length))-1)
    #define LoopForwardLengthFromZero(var,length) LoopForward(0,var,(length)-1)
    #define LoopBackwardLengthFromZero(var,length) LoopBackward(0,var,(length)-1)

  6. #6
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,632

    Re: [beginner] Include problems tidying up code

    I don't see how those macros makes the code cleaner. In my opinion all they do is to make things more obfuscated. Do you have macros like
    Code:
    #define DIVIDE(x,y) (x/y)
    #define ASSIGN(x,y) (x=y)
    #define COMPARE(x,y) (x==y)
    as well?
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    http://www.microsoft.com/visualstudio/eng/downloads

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center