Menu

using header files...begginer question

Labmouse
2007-09-03
2012-09-26
  • Labmouse

    Labmouse - 2007-09-03

    Hi having only recently started programming in C++ i have got to the stage that i have made my own header file but cant get it to work.It is saved where the rest of my source is.And shows up there as a headerfile(although it says its C and not C++?)When I include it in my main none of the functions work.

    Im using Dev-C++ ver.4.9.9.2
    windows vista.

    Heres the header file code in case the fault is there:

    ifndef myfunctions.h

    define myfunctions.h

    //diceRoll function
    int diceRoll( int sides, int num_dice )
    {
    int sum = 0;
    for( int i = 0; i < num_dice; ++i)
    sum += rand() % sides + 1;

    return sum;
    

    }

    //playerdam
    int playerdam(int &gobHealth)
    {
    int dam=diceRoll(10,1);
    gobHealth-=dam;
    return dam;
    }

    //gobdam
    int gobdam(int &playerHealth)
    {
    int dam=diceRoll(10,1);
    playerHealth-=dam;
    return dam;
    }

    endif

    and in my main i try and include it :

    include<iostream>

    include<ctime>

    include<myfunction.h>

    I hope i have explained it ok.any help much appreciated

    lbmouse

     
    • Labmouse

      Labmouse - 2007-09-03

      Thanks old newbie that done the trick.Does it matter that my header file is showing as a C header file and not a C++ one?

      Labmouse

       
      • Anonymous

        Anonymous - 2007-09-03

        No. If you include it in a .cpp file and teh C++ compiler is used to compile it, it is a C++ header! You could use .hpp as the extension if you wish, but few use that convention.

         
    • Labmouse

      Labmouse - 2007-09-03

      Thanks again,

      Labmouse

       
    • Anonymous

      Anonymous - 2007-09-03

      Try

      include "myfunction.h"

      Good luck

      Old Newbie

       
    • Anonymous

      Anonymous - 2007-09-03

      include <...> will only look in the folders specified by -I compiler options, wheras #include "..." will look in the folder containing the file being compiled first.

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.