Functions, some strange thing about them to me, long description inside.

This is a discussion on Functions, some strange thing about them to me, long description inside. within the C++ Programming forums, part of the General Programming Boards category; Hi everyone. I didn't knew how to formulate this in a short sentence, so here's the question: I have a ...

  1. #1
    Novice programmer newn's Avatar
    Join Date
    Aug 2010
    Posts
    59

    Functions, some strange thing about them to me, long description inside.

    Hi everyone. I didn't knew how to formulate this in a short sentence, so here's the question:
    I have a func.h file. In that file, I've the main game loop, logic, declaring images and sounds, all the stuff. I also have main.cpp file. So now i have two functions inside those two files: main(); and game_loop();.
    When i put the code (just copy-paste) from the main(); to game_loop(); and add game_loop(); (Yea, just line like that, nothing more.) into main();, it doesn't work. And while it's still in main();, it works perfectly fine.

    Example, if I've confused you:

    Code:
    Working example, only main.cpp file is in the project:
    
    int main()
    {
    lots of code...
    }
    
    Not working example, main.cpp and func.h is in the project:
    func.h:
    void game_loop()
    {
    Lots of code.
    }
    main.cpp:
    #include "func.h"
    int main()
    {
    game_loop();
    }

  2. #2
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,641
    You've not provided enough information, starting with how it "doesn't work"!

  3. #3
    Novice programmer newn's Avatar
    Join Date
    Aug 2010
    Posts
    59
    Oh. Wrote and forgot to write the most important thing. Sorry for that, haven't slept much today.

    Anyway, it doesn't drawn images. I'm using SFML in addition to C++.
    Posted here, since before, it wasn't recognizing the code, now it magically recognized it, while i did nothing. Also, there were some problems compiling, which are also gone now. So only this drawing stuff left. I guess i should ask in SFML's forums about this then.

  4. #4
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,403
    what have you put a load of working code like that into the header file for? Keep your header file to mostly just declarations and descriptions, move the working code into a new source file instead,
    and are you delclaring main() twice in different files or what is going on there?
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  5. #5
    Novice programmer newn's Avatar
    Join Date
    Aug 2010
    Posts
    59
    No, my declarations are okay.

    Hmmm, but what if i have multiple logic codes? One for menu, another for the game for example? How can i "divide" it without header files? Use functions or something?

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,600
    Quote Originally Posted by newn View Post
    No, my declarations are okay.

    Hmmm, but what if i have multiple logic codes? One for menu, another for the game for example? How can i "divide" it without header files? Use functions or something?
    Ideally, your header files should contain very little code - prototypes, forward declarations, etc. First compile the code for the header files separately, and then pass the resulting object files to the linker (using a make file or an IDE, preferably). That's the traditional way to do it, anyway. In any case, be sure to understand the linking process...you'll need that, eventually!
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  7. #7
    Novice programmer newn's Avatar
    Join Date
    Aug 2010
    Posts
    59
    Ummm, not sure what you mean by "first compile header files separately, and then pass the resulting object files to the linker". Are talking about my own library creation for my needs with classes, or i don't udnerstand something here?

  8. #8
    Registered User
    Join Date
    Mar 2008
    Location
    Coimbra, Portugal
    Posts
    85
    The idea is that header files contain only declarations.

    In practice, you define your code in several source-code files (.c, cxx, .cpp) that get linked in the final output. So, for instance:

    main.cxx
    Code:
    #include<A.h>
    #include <B.h>
    //...
    A();
    B();
    //...
    A.h
    Code:
    void A(void);
    A.cxx
    Code:
    void A(void) { /* code */}
    B.h
    Code:
    void B(void);
    B.cxx
    Code:
    void B(void) { /* code */}
    So, you see, A and B are declared in separate header files, but the code for the functions themselves is implemented / defined in other .cxx files. Then, when compiling, you'll link them all together.

    For instance, when you #include <windows.h>, you're not including the actual code fo windows core functions -- you're including many of their declarations. The real code is linked later on in the form of static libraries, which are simply more object code.

    Having all code definitions in a header is really bad. Firstly, because it'll always be compiled when you recompile something that includes it. Secondly, because, since the header file contains definitions, every single different file that includes it will be defining them, so you'll get multiple definitions and link-time errors.

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,182
    Quote Originally Posted by Jorl17 View Post
    Secondly, because, since the header file contains definitions, every single different file that includes it will be defining them, so you'll get multiple definitions and link-time errors.
    Solved by marking the functions as inline. Though it still doesn't make it a good idea generally. All the other points are still also valid.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  10. #10
    Registered User
    Join Date
    Mar 2008
    Location
    Coimbra, Portugal
    Posts
    85
    Quote Originally Posted by Elysia View Post
    Solved by marking the functions as inline. Though it still doesn't make it a good idea generally. All the other points are still also valid.
    You're right, I'd forgotten about that.

  11. #11
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    404
    Wow this thread just took away 6 months of how I've been programming lol... I will difinitely start using this method I have previously been making games that has a .h that defines each chunk of functions used then the main just includes those .h.... obviously if it is bad practice I would like to get out of this habit, can somone elaborate on the method of linking the files to your project afterwards.

    I am assuming this is what I've been doing when I learned SDL and OpenGL and had to memorize the linkerers required for each particular .h that related to those. I would just like to know if I made a welcome.h for a game that declared all the functions neeeded for the welcome screen to a game.. aka intro, load game, new game.. then defined these in a welcome.cxx what would I put in my linkerer section of the project file to link welcome.cxx?

    And lastly why .cxx and not .cpp?

  12. #12
    Registered User
    Join Date
    Mar 2008
    Location
    Coimbra, Portugal
    Posts
    85
    Quote Originally Posted by Lesshardtofind View Post
    Wow this thread just took away 6 months of how I've been programming lol... I will difinitely start using this method I have previously been making games that has a .h that defines each chunk of functions used then the main just includes those .h.... obviously if it is bad practice I would like to get out of this habit, can somone elaborate on the method of linking the files to your project afterwards.

    I am assuming this is what I've been doing when I learned SDL and OpenGL and had to memorize the linkerers required for each particular .h that related to those. I would just like to know if I made a welcome.h for a game that declared all the functions neeeded for the welcome screen to a game.. aka intro, load game, new game.. then defined these in a welcome.cxx what would I put in my linkerer section of the project file to link welcome.cxx?
    What IDE / compiler/linker are you using? If you're with MSVC++, just add the file to the project and it should automatically get linked. If you're not using an IDE in a Unix system (for instance, with gcc, g++ and ld), you'll have to adapt whatever you're doing. If you're first compiling, then linking, which I doubt, you'd have to compile the .cxx file and then add the resulting .o file at the end of the linker. If you're directly compiling using g++ <file_list> -o <output>, then just add your file in the file list.

    Usually, most modern IDEs take care of that for you.


    Quote Originally Posted by Lesshardtofind View Post
    And lastly why .cxx and not .cpp?
    I think I started using that convention because some editor of mine didn't understand .cpp. But, nowadays, I usually use .cxx for any class implementation file, .cpp for other c++ files, .h for basically every header and .c for the average C file. It really is your choice

  13. #13
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,182
    I don't think most IDEs will understand .cxx because it's not a standard extension. Any editor that does not recognize .cpp needs to be shot.
    .cpp for implementation and .h(pp) for headers.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  14. #14
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    404
    I'm scared to say I'm using Dev C++ because I get yelled at on this board everytime I say it, but that is my current compiler, and it does do these for me. It even apparently lets me browse for the files I want to link. The reason I asked was to get a grasp of the subject on the floor level without letting the compiler do it for me. Say my linkerers for a common SDL project have

    -lSDLmain -lSDL -lSDL_image

    I was wondering if there was universal system of linking that this was following. How do you make a .cxx a .o or .lib in order for me to add it via the "browse" option built into my compiler?

  15. #15
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,182
    The compiler takes your source files and translates them into object files (typical .o extension). Linker takes these (object files) and lib files (.lib) and puts them into a final exe. That's how it works.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

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

Similar Threads

  1. GCC - Strange networking functions error.
    By maththeorylvr in forum Windows Programming
    Replies: 3
    Last Post: 04-05-2005, 12:00 AM
  2. A very strange thing
    By gustavosserra in forum C++ Programming
    Replies: 4
    Last Post: 04-15-2003, 12:43 PM
  3. strange strange functions
    By threahdead in forum C Programming
    Replies: 4
    Last Post: 10-13-2002, 05:31 PM
  4. Strange thing happens...
    By funkydude9 in forum C++ Programming
    Replies: 3
    Last Post: 09-10-2002, 07:56 PM
  5. a strange thing
    By SuperNewbie in forum C# Programming
    Replies: 2
    Last Post: 07-14-2002, 10:06 AM

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