Free function compiling error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • TheMusician
    New Member
    • Sep 2011
    • 1

    #1

    Free function compiling error

    Error:

    Code:
    Undefined                       first referenced
     symbol                             in file
    die()                               /var/tmp//cckddR2X.o
    ld: fatal: symbol referencing errors. No output written to a.out
    collect2: ld returned 1 exit status
    This is my die.cpp:

    Code:
    #include <math.h>
    #include <string>
    #include <iostream>
    #include <fstream>
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include "die.h"
    
    
    
    int die(){
    
            srand (time(NULL));
            int number = rand() % 6 + 1;
    
            return number;
    
    }
    This is my die.h:

    Code:
    #ifndef __DIE_H
    #define __DIE_H
    
    
    int die();
    
    
    #endif
    I'm not sure what I'm doing wrong. Did I leave something out of my .h file? Any feedback is appreciated.
  • johny10151981
    Top Contributor
    • Jan 2010
    • 1059

    #2
    your code is fine. what is your compilation command???

    i mean
    Code:
    #g++ -c die.cpp -o die
    if it is not then, you are getting error because you dont have define main function

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      I don't know that this has anything to do with your link error, but I believe you should be including <cmath> instead of <math.h>, <cstdio> instead of <stdio.h>, <cstdlib> instead of <stdlib.h>, and <ctime> instead of <time.h>.

      This doesn't have anything to do with your link error, but I believe you should say "int die(void)" instead of "int die()" in die.h and die.cpp.

      I doubt this has anything to do with your error, but you should not get in the habit of using names with leading underscores (__DIE_H). The C Standard (and perhaps the C++ Standard) reserves all such names for use by behind-the-scenes compiler internals. Change this name to something like DIE__H.

      You might verify that "die" isn't a reserved word or name by changing all instances of "die" in die.h and die.cpp to something else, perhaps "xxdie" and confirm that the error still occurs. No need to change the file names.

      Comment

      Working...