Thread: i hate it when C++ fails to make any sense...

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    465

    i hate it when C++ fails to make any sense...

    im trying to create a simple header file for a couple custom functions.

    Code:
    #include <iostream.h>
    #include <conio.c>
    #include <time.h>
    
    void pause(int t);
    int random(int u);
    
    
    void pause(int t){
         int x;
         while ( x < 1000000000 ){
         x = x + t;
         }
    }
    
    
    int random(int u){
        int x;
        srand(time(NULL));
        x = ( rand() % u ) + 1;
        return x;
    }
    but when i go to compile the program, it gives me the errors:

    redefinition of `void pause(int)'
    `void pause(int)' previously defined here
    redefinition of `int random(int)'
    `int random(int)' previously defined here

    and when i doubleclick the errors to see where they are, they both highlight the same line of code. basically, its telling me that those functions are previously defined by themselves! ive tried everything i can think of. whats the deal??
    Last edited by ...; 11-06-2002 at 05:52 PM.
    I came up with a cool phrase to put down here, but i forgot it...

  2. #2
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Code:
    void pause(int t){
         int x;
         while ( x < 1000000000 ){
         x = x + t;
         }
    }
    x is undefined, therefore the behavior of this function is undefined.

    Also, why do you need function prototypes if you flesh out the function directly underneath? It seems pointless to me.

    Another thing, create an inclusion guard for your header...

    #ifndef __RANDOM_PAUSE_H
    #define __RANDOM_PAUSE_H

    // .. code ..

    #endif

    edit: I think those names may already be taken... especially random(). Use a namespace or rename your functions.

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    465
    Originally posted by Eibro
    why do you need function prototypes if you flesh out the function directly underneath? It seems pointless to me.
    ive heard its good practice. and plus, when i expand the files more i like to see at a glance which functions ive included in which header/cpp files.

    Originally posted by Eibro
    Another thing, create an inclusion guard for your header...

    #ifndef __RANDOM_PAUSE_H
    #define __RANDOM_PAUSE_H

    // .. code ..

    #endif
    im not the most experienced programmer, and im not quite sure how to use inclusion guards or what the purpose of them is.

    Originally posted by Eibro
    edit: I think those names may already be taken... especially random(). Use a namespace or rename your functions.
    well, the pause() function worked when i had it in my main cpp file. as a matter of fact, it was even working at one point while it was in its current state. i just changed 'random()' to 'randomnumber()' and im still having the same problems.
    I came up with a cool phrase to put down here, but i forgot it...

  4. #4
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Try this:

    Code:
    #include <iostream.h>
    #include <conio.c>
    #include <time.h>
    
    #ifndef __RANDOM_PAUSE_H
    #define __RANDOM_PAUSE_H
    
    void pause(int t);
    int random(int u);
    
    
    void pause(int t){
         int x = 0; // Notice this
         while ( x < 1000000000 ){
         x = x + t;
         }
    }
    
    
    int random(int u){
        int x;
        srand(time(NULL));
        x = ( rand() % u ) + 1;
        return x;
    }
    
    #endif

  5. #5
    Registered User
    Join Date
    Feb 2002
    Posts
    465
    that works... thanks. now can you explain to me what i just did?

    (btw, when you create an int and dont assign it a number, it defaults to 0. the pause function works without setting x to 0)
    I came up with a cool phrase to put down here, but i forgot it...

  6. #6
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Originally posted by Eibro

    Also, why do you need function prototypes if you flesh out the function directly underneath? It seems pointless to me.
    It's quite useful, avoids problems if you misspell the function name. Bjarne Stroustrup explains this further in his book "The C++ Programming Language".

    Actually CodeWarrior issues a warning if a function is defined without being declared.

    (btw, when you create an int and dont assign it a number, it defaults to 0. the pause function works without setting x to 0)
    Don't rely on it.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  7. #7
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Not on my compiler/system does an undefined value default to 0. Try:
    Code:
    int x;
    cout << x;
    What that inclusion guard does is defines the macro __RANDOM_PAUSE_H if it's not already defined, and includes the code in between the #if .. #endif macros at compile time. However, if the macro is defined... it your code is not included in the build. This results in a "guard" which prevents your header from being included twice, no matter how many times you actually include it.

    Someone can probably explain it better than I.
    Last edited by Eibro; 11-06-2002 at 06:17 PM.

  8. #8
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Originally posted by ...

    (btw, when you create an int and dont assign it a number, it defaults to 0
    only in static storage, not here
    hello, internet!

  9. #9
    Registered User
    Join Date
    Feb 2002
    Posts
    465
    Don't rely on it.
    Not on my compiler/system does an undefined value default to 0
    only in static storage, not here
    ive been beaten into submission

    and thanks, eibro. i learned something new today.
    I came up with a cool phrase to put down here, but i forgot it...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Win32 Common Controls in C++, how do i make and use them?
    By C+noob in forum Windows Programming
    Replies: 6
    Last Post: 01-09-2006, 11:53 AM
  2. Does this even remotely make sense
    By hckr83 in forum C Programming
    Replies: 6
    Last Post: 12-23-2005, 05:02 PM
  3. Pet Peeves
    By Srg Pepper in forum A Brief History of Cprogramming.com
    Replies: 29
    Last Post: 10-03-2002, 11:34 AM
  4. WinXP Upgrade Doesn't Make Any Sense
    By Troll_King in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 10-25-2001, 04:18 PM
  5. anyone make any sense of this
    By Unregistered in forum C++ Programming
    Replies: 6
    Last Post: 10-03-2001, 10:29 PM