Thread: Need HELP!

  1. #1
    Registered User IndioDoido's Avatar
    Join Date
    Apr 2007
    Posts
    194

    Unhappy Need HELP!

    hi!

    I'm having a problem on calling a function inside another function.
    All my functions are in header files.

    I've already called the menuInicial() function in the main() program, and it worked.
    But since i tryed to call it in the menuLogin() function it's giving me this error:
    "menuinicial.h(5) : error C2371: 'menuInicial' : redefinition; different basic types"

    I'm using VS2005
    Can anyone help me?
    Last edited by IndioDoido; 04-16-2007 at 12:02 PM.

  2. #2
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Inside each of your headers you must put 'header guards' (I think that's what they're called), which basically makes sure that your code doesn't get included multiple times, which is your problem, I think.

    put this at the start of your code:

    #ifndef SOMETHING
    #define SOMETHING

    /* insert code here */

    #endif
    the "SOMETHING" can be anything. What I normally do is TWOMERS_HEADERNAME to ensure that there isn't another preprocessor definition of that name. So for your "menuInicial.h" header I'd put:

    #ifndef TWOMERS_MENUINICIAL
    #define TWOMERS_MENUINICIAL

    /* YOUR CODE HERE */

    #endif
    Repeat, rinse and lather

  3. #3
    Registered User IndioDoido's Avatar
    Join Date
    Apr 2007
    Posts
    194
    hey twomers, tnx for the fast reply.

    I've done what you told me, but it's not working :-S
    Last edited by IndioDoido; 04-16-2007 at 12:02 PM.

  4. #4
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Have you done that for all the headers? cxSeparadora.h and ControlomenuInicial.h too?

    You can change the TWOMERS to something else, by the way. It's not bound to my alias

  5. #5
    Registered User IndioDoido's Avatar
    Join Date
    Apr 2007
    Posts
    194
    hep!
    hehee! I know...i didn't change it because i wanted to test it first ;-)

    I did it in all of them:
    "ControlomenuInicial.h", "cxSeparadora.h", "menuInicial.h", "menuLogin.h", "titulos.h" and nothing

  6. #6
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Did you clean and rebuild it all?

  7. #7
    Registered User IndioDoido's Avatar
    Join Date
    Apr 2007
    Posts
    194
    yes...lots o times.


    I even added the #ifndef and #define in the main program.
    Last edited by IndioDoido; 04-15-2007 at 02:01 PM.

  8. #8
    Registered User IndioDoido's Avatar
    Join Date
    Apr 2007
    Posts
    194
    Any more ideias?
    I even wrote the program from the begining

  9. #9
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by IndioDoido View Post
    All my functions are in header files.
    This is generally something to avoid. That is, don't put function or data definitions in headers. Headers are a place for declarations, i.e. function prototypes, etc.

    The header guards will only help you so far along -- with compiling, and then you'll bump into a linker issue if the header is included in multiple source files.

    Look up all references to menuInicial. See if the prototype differs from how it is used. Or whether is called from a module that does not include the header, thus defaulting it to an int return type.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  10. #10
    Registered User IndioDoido's Avatar
    Join Date
    Apr 2007
    Posts
    194
    hi Dave...

    I'm using header files so that my project can be more organized. :-S

    The compile only shows the error when i call the menuInicial() in the menuLogin() function.
    Besides that, the prog runs nicely.

    Maybe you can find where the problem is.
    Last edited by IndioDoido; 04-16-2007 at 06:58 AM.

  11. #11
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by IndioDoido View Post
    I'm using header files so that my project can be more organized. :-S
    That's the right idea, but you're doing it wrong. I was trying to let you in on this before you get used to doing it this way.

    One of the issues is that you call a function without #include-ing the header, as I also mentioned; void menuLogin(void) impliticly declares your function returning an int.

    You're running into these problems because you have code in header files. You will continue you have problems as long as you continue to do so.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  12. #12
    Registered User IndioDoido's Avatar
    Join Date
    Apr 2007
    Posts
    194
    ohhhh...

    I removed the void from the void menuLogin(void), but it didn't resolve the error; but when i removed the void from void menuInicial(void), (menuInicial(void)) it resolved the problem.

    I didn't #include "menuInicial.h" in the menuLogin header because it was giving me about 600warnings lolol, but after I removed the void from menuInicial the compile didn't give any errors.


    "You're running into these problems because you have code in header files. You will continue you have problems as long as you continue to do so."

    What do you mean by having code in the header files?

  13. #13
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by IndioDoido View Post
    "You're running into these problems because you have code in header files. You will continue you have problems as long as you continue to do so."

    What do you mean by having code in the header files?
    In a header file, just include function prototypes (and if you REALLY, REALLY HAVE TO, global variables).

    Do not actually write the function out inside the .h file. Define it in the equivalent .c file.

    So for an example:

    Code:
    /* swap.h */
    #ifndef SWAP_DEF
    #define SWAP_DEF
    
    void swap(int *, int *);
    
    #endif
    Code:
    /* swap.c */
    #include "swap.h"
    
    void swap(int *a, int *b)
    {
    	int temp = *a;
    	*a = *b;
    	*b = temp;
    }

  14. #14
    Registered User IndioDoido's Avatar
    Join Date
    Apr 2007
    Posts
    194
    ahhhhhhhhhh!!!!

    ok...now i get it

    i forgot about that method...i'm realy needing some practice

    Tnx alot you guys! you have all been a great help, this forum is going to be cyber home from now on

    once again tnxxxxxxx

Popular pages Recent additions subscribe to a feed