1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Development Free function compiling error in C++

Discussion in 'Tech Support' started by TheMusician, 30 Sep 2011.

  1. TheMusician

    TheMusician Audio/Tech Enthusiast/Historian

    Joined:
    13 Jul 2009
    Posts:
    573
    Likes Received:
    32
    Tried to compile a free function (die.cpp and die.h) in Unix terminal and I can't seem to get it to work.

    The 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
    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;
     
    }
     
    The 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.
     
  2. Dae314

    Dae314 What's a Dremel?

    Joined:
    3 Sep 2010
    Posts:
    988
    Likes Received:
    61
    You need an int main() function in your cpp since your'e trying to run from it. To define it really quickly, just say:

    Code:
    int main() {
        die()
    }
    You don't need to put the main function in the header file.

    I'm not sure if you actually need this in a .h file, but I usually just put using namespace std;
     
  3. GoodBytes

    GoodBytes How many wifi's does it have?

    Joined:
    20 Jan 2007
    Posts:
    12,300
    Likes Received:
    710
    Yup, I am sure adding the main function will do the trick.

    "main" function is the first function being executed when you run any C++ program.
    (well almost all programming languages have a main sort of function, can be called differently).
     
  4. azrael-

    azrael- I'm special...

    Joined:
    18 May 2008
    Posts:
    3,852
    Likes Received:
    124
    A main() function is needed, but you also have to make sure you provide the correct signature.

    Something like this should do the trick:


    Code:
    int main(int argc, char *argv[])
    {
      printf("The die was cast and the result is %d\n". die()); // To actually see the result of your function
    
      return 0; // Return code: 0 means the program returned without error.
    }
     

Share This Page