Thread: problems with header files

  1. #1
    -
    Join Date
    Feb 2003
    Posts
    47

    problems with header files

    I have 3 files
    main.cpp
    random.cpp
    random.h

    they contain this:

    main.cpp -
    Code:
    #include <iostream>
    #include <cstdlib>      // library for rand(); function
    #include "random.h"     // My library
    
    using namespace std;
    
    int main()
    {
    
        random(1,2);
                                     
     	
        return 0;
    }
    random -
    Code:
    #include <iostream>
    #include <cstdlib>      // library for rand(); function
    #include "random.h"     // My library
    #include <sys/types.h>  // library for time_t time(time_t*);
    
    using namespace std;
    
    int random(int argument1, int argument2)
    {
        int returnValue = 0; 
        time_t time(time_t*);   // seed for rand();
        
        // test purpose 
        int sum = 0;
        sum = argument1 + argument2;
        cout << "rand() values:\n";
        for (int i = 0; i < 50; i++) {
            cout << rand() % sum << ' ';
        }        
        
        
        system("PAUSE");
        return returnValue;
    }
    and my problem
    random.h -
    Code:
    int random(int argument1, int argument2);
    When i try to compile (even though its barely a program) i get this error.

    In function `int main()':
    `random' undeclared (first use this function)
    Makefile.win [Build Error] [ass2.o] Error 1

    I have a good idea what this means but i have _no_ idea how to fix this. Ive searched but my google skills are lacking. Hope anyone can help

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Hmmm what compiler are you using all seems to compile for me
    Woop?

  3. #3
    -
    Join Date
    Feb 2003
    Posts
    47
    Dev-C++ 4.9.9.0 the latest version, i might need to go back one coz i just tried it on a solaris 5.* box and it had a few warnings like double lined comment? (dunno?) but compiles... weird

  4. #4
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    I am using Dev 4.9.9.0 also. Weird, maybe a bad install?
    Woop?

  5. #5
    -
    Join Date
    Feb 2003
    Posts
    47
    Argh,
    Uninstalled 4.9.9.0 and reinstalled it, same problem
    Went back to 4.9.8.0 and it has the same again.

  6. #6
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Are you doing this as a project or as just a single file?
    Woop?

  7. #7
    -
    Join Date
    Feb 2003
    Posts
    47
    Project actually, this is the first time ive needed to use more then one file (if you get me) and i did it as a project. ill try it another way

  8. #8
    ---
    Join Date
    May 2004
    Posts
    1,379
    i havent look at you code properly, but i had the same error in dev-c++ earlier today.
    i have this
    Code:
    #include <stdlib.h>
    #include "SDL/SDL.h"
    #include "main.h"
    my problem was that, before i fixed it, i had main.h declared before SDL.h and in main.h there were declarations to types defined in SDL.H

    try and include random.h last, after all the others.

  9. #9
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Here i bundled up a project for you this one compiles for me see if it will work for you
    Woop?

  10. #10
    -
    Join Date
    Feb 2003
    Posts
    47
    Thanks but nah does not seem to work, this has me totally baffled. Ill just use the painful unix box untill i can figure it out dont have time to keep playing with it at the moment, thanks for all your help people.

  11. #11
    -
    Join Date
    Feb 2003
    Posts
    47
    Thats insane it works when you code it.
    Ill just keep going with these files.. heh thanks mate *scratches head*

  12. #12
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Maybe somehow your files were mixed up thats probably the most resonable explanation
    Woop?

  13. #13
    -
    Join Date
    Feb 2003
    Posts
    47
    Update:

    When added a comment to your code and tried to recompile it died - Just for reference, future problems.

    [Linker error] undefined reference to `__gxx_personality_v0'

    resonable explanation is that dev C++ == cBugBug
    Lets end this topic here it seems like it will go on forever.

  14. #14
    Registered User
    Join Date
    Jul 2004
    Posts
    98
    Can you show me your random.h?
    Let's check it?

  15. #15
    -
    Join Date
    Feb 2003
    Posts
    47
    my random.h consists of one line the same as the first post. Its just a prototype of one function, exactly the same code has given me 3 different errors, on the one line where i call the function. its been so random i cant make a base problem where i can work from to come to a solution.

    [edit]
    the 3 problems were under different conditions, as above diff editor versons etc
    [/edit]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Novice question on header files
    By hern in forum C++ Programming
    Replies: 1
    Last Post: 07-30-2005, 10:11 AM
  2. header file problems
    By noob2c in forum C Programming
    Replies: 9
    Last Post: 04-08-2003, 05:33 PM
  3. Header files
    By GaPe in forum C Programming
    Replies: 6
    Last Post: 02-01-2003, 03:39 PM
  4. Using c++ standards
    By subdene in forum C++ Programming
    Replies: 4
    Last Post: 06-06-2002, 09:15 AM
  5. Header files
    By Jez_Master in forum C++ Programming
    Replies: 2
    Last Post: 04-08-2002, 07:56 AM