Thread: C++ Funtion/Include Problems.

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    26

    C++ Funtion/Include Problems.

    Hi to all, I'm new here!

    I'll give you a quick rundown of what I am trying to achieve.

    A short while ago, I set about learning my first scrap of C++ coding, And have manage to successfully code a few simple programs, such a quiz's, add libs, etc. Now I am starting on my very own game, an RPG. I know it's a lot to dive in to right now, but I'm determined at heart.

    Anyway, What I have a problem with is the funtionality of things such as

    #include <includefile.h> etc.

    What I am trying to do, is simple. I want a header file, that contains the following -

    Mana = Intelligence X 2

    And another file, Orc, which passes the variable Intelligence, to the header file, then the header file returns the variable, Mana.

    here's the code for the orc (orc.cpp)

    Code:
    //
    // RPG ORC
    // 
    //
    #include <cstdio>
    #include <cstdlib>
    #include <iostream>
    #include <statcheck.h>
    #include <string.h>
    using namespace std;
    
    //Prototype Declarations
    
    
    int main(int nNarg, char* pszArgs[])
    { 
       
       int vitality = 10;
       int strength = 10;
       int intelligence = 2;
       
       int hitpoints = 1;
    
       
      
       
    
       cout << vitality << "\n"; 
       cout << strength << "\n";
       cout << intelligence << "\n";
      
       cout << hitpoints << "\n";
       
       
       
     
        //wait until user is ready before terminating the program
        // to all user to see the program results
        system("pause");
        return 0;
    }
    And here's the code for the header file (statcheck.h)
    Code:
    //
    // Mana
    // 
    //
    #include <cstdio>
    #include <cstdlib>
    #include <iostream>
    #include <string.h>
    using namespace std;
    
      
          
        
    
    int main(int intelligence)
    { 
      int mana  = intelligence * 2;
        cout << mana << "\n";
      
        
        //wait until user is ready before terminating the program
        // to all user to see the program results
        system("pause");
        return 0;
    }
    I'm not sure what the heck i'm doing, so be kind

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    You only have 1 main function per program. I think what you're looking for is a function:
    Code:
    float GetMana(float Intelligence)
    {
      return Intelligence * 2;
    }
    
    int main()
    {
      ...
    
       ... = GetMana(3);
      ...
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    1. Headers should really only contain function prototypes, typedefs and things of that nature. Code should not come into play with headers unless you are dealing with templated code (which you are not).

    2. A program cannot have two main functions. Your linker will complain when you attempt to build the program. The code that returns the "mana" should be a function called perhaps GetMana that accepts an int value denoting intelligence. All it needs to do is return twice the value that is input as the argument. The actual code for the function should be in a seperate source file, perhaps called StatCheck.Cpp or something.

    I would alter your setup to something approaching the following:

    Code:
    //StatCheck.H
    #ifndef STATCHECK_H
    #define STATCHECK_H
    int GetMana(int);  // Function prototype
    #endif
    
    
    //Orc.Cpp
    #include <cstdio>
    #include <cstdlib>
    #include <iostream>
    #include "statcheck.h"
    #include <string.h>
    using namespace std;
    
    int main(int nNarg, char* pszArgs[])
    { 
       
       int vitality = 10;
       int strength = 10;
       int intelligence = 2;
       
       int hitpoints = 1;
    
       cout << "Vitality: "  << vitality << "\n"; 
       cout << "Strength: " << strength << "\n";
       cout << "Intelligence: " << intelligence << "\n";
       cout << "Hit Points: " << hitpoints << "\n";
       cout << "Mana: " << GetMana(intelligence) << "\n";
       
       
     
        //wait until user is ready before terminating the program
        // to all user to see the program results
        system("pause");
        return 0;
    }
    
    
    // StatCheck.Cpp
    int GetMana(int intelligence)
    { 
        return intelligence * 2;
    }
    You now have a multi-sourcefile project that would require you to compile the two source files into object files and then link the two object files together to produce an executable program. How you do that depends on your available tools such as what compiler are you using? Is it an IDE or are you using a command-line compiler.

    In the future such a program above would typically be done by creating a base "player" class and having such things as the GetMana function be a member function of the class. You would derive other classes such as Orc and maybe Dwarf and Human from this base "player" class.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Apr 2005
    Posts
    26
    Hmmm, not sure we are on the same page.

    I want to have a file, call it monster X, which uses a file, Statcheck to determin the amount of mana it has.

    I also want to be able to use Statcheck, for other monsters, e.g. Monster Y, Monster Z.

    EDIT: Some one posted before I just did, this above post may be null and void!

    ok... I'm having a difficult time with this, i'm sure if I get it once, It'll click though.

    Could you define "Function Prototype"
    Last edited by Smeep; 04-21-2005 at 02:06 PM.

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    1) No header between angled brackets, e.g.:

    #include <string.h>

    should ever have a .h extension. Only your headers, which are declared like this:

    #include "myheader.h"

    should have a .h extension. The line:

    #include <string.h>

    should be:

    #include <cstring>

    but since you aren't using any functions defined in <cstring>, you don't even need it.

    2) An include statement just takes whatever is in the included file and inserts it at the spot of the include statement. So, while it may be good practice to just include function prototypes, class definitions, etc. in a .h file, you can put function definitions in there too.

    Generally, you will just put your function prototypes and class declarations in an include file. If you don't include the function prototypes and class declarations before main(), and then you mention any of those function names or classes in main(), you will get an error. The compiler will shout out: "where the heck did this function name come from. I didn't see it defined anywhere!". It's similar if you do this:

    n= 10;

    The compiler will holler: "What is 'n'!". You have to declare n first:

    int n;
    n=10;

    When you just include function prototypes and not the function definitions, you are telling the compiler: "Hey bud, here are some names you are likely to see down in main(). I haven't defined them for you here, but don't freak out, they are in some other .cpp files, which your buddy Mr. Linker will send to you as soon as he can. In the meantime, go ahead and compile the rest of the code." Because Mr. Compiler appreciates cordial treatment, he assents and admonishes you--"don't forget to put those function definitions in a .cpp file somewhere, or I'll make sure Mr. Linker stops you flat in your tracks." If you heed that warning and put your defintions in a .cpp file AND you include the header file in the .cpp file, Mr. Linker will sort everything out.
    Last edited by 7stud; 04-21-2005 at 02:36 PM.

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Could you define "Function Prototype"
    function:
    Code:
    int myFunc(int n)
    {
          cout<<n<<endl;
    }
    function prototype:

    int myFunc(int n);
    Last edited by 7stud; 04-21-2005 at 02:16 PM.

  7. #7
    Registered User
    Join Date
    Apr 2005
    Posts
    26
    Cheers.

    Right, I'm getting the following error right now,

    "9 C:\Dev-Cpp\CPP_Programs\Chap01\rpg\orc.cpp
    declaration of C function `int "
    When I try to compile it.

  8. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    1)Post your code
    2)Post the error
    3)Put a big red: THE ERROR IS HERE on the line with the error.

    I also added some details to the bottom of my previous post that you might want to read.

  9. #9
    Registered User
    Join Date
    Apr 2005
    Posts
    26
    Code:
    #include <cstdio>
    #include <cstdlib>
    #include <iostream>
    #include "StatCheck.H"
    #include <cstring>
    using namespace std;
    
    int main(int nNarg, char* pszArgs[])
    { ERROR IS HERE
       
       int vitality = 10;
       int strength = 10;
       int intelligence = 2;
       
       int hitpoints = 1;
    
       cout << "Vitality: "  << vitality << "\n"; 
       cout << "Strength: " << strength << "\n";
       cout << "Intelligence: " << intelligence << "\n";
       cout << "Hit Points: " << hitpoints << "\n";
       cout << "Mana: " << GetMana(intelligence) << "\n";
       
       
     
        //wait until user is ready before terminating the program
        // to all user to see the program results
        system("pause");
        return 0;
    }
    Error at the bottom reads:

    [Warning] In function `int main(int,:
    declaration of C function `int
    16 previous declaration `int this line is highlighted
    [Warning] In function `int main(int)':
    21 `GetMana' undeclared (first use
    21 (Each undeclared identifier is

  10. #10
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    1) Get rid of "int nNarg, char* pszArgs[]"

    Your basic main() function should look like this:
    Code:
    int main()
    {
    
         return 0;
    }
    2) The compiler doesn't know what the GetMana() function is. Did you include any header file that has that function prototype or function definition in it? If the included file has the GetMana() prototype in it, did you create a .cpp file with the GetMana() function definition in it, AND did you include the header file in that .cpp file?
    Last edited by 7stud; 04-21-2005 at 02:50 PM.

  11. #11
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    3) Stop everything you are doing with this program. Close the project. Start a new project. In your new project, create three files:

    a) main.cpp
    b) myfunction.h
    c) myfunction.cpp


    Your main.cpp file will have main() in it. Call a simple function in main(), like this:

    Code:
    int main()
    {
    	someFunc();
    	
    	return 0;
    }

    Include myfunction.h at the top of main.cpp:

    Code:
    #include "myfunction.h"
    
    int main()
    {
    	someFunc();
    	
    	return 0;
    }

    In myfunction.h, put the function prototype:

    //myfunction.h
    #ifndef __MYFUNCTION_H__
    #define __MYFUNCTION_H__

    void someFunc();

    #endif //__MYFUNCTION_H__

    Those lines around the function prototype keep you from including the file twice somewhere, which will cause an error. Having those lines is standard practice for .h files.

    In myfunction.cpp put the function definition along with the proper includes:

    Code:
    #include <iostream> //for cout, endl
    #include "myfunction.h" //mandatory
    
    using namespace std;
    
    void someFunc()
    {
    	cout<<"hello world"<<endl;
    }
    If you can get that structure to work, you should have a better idea of how to do it with your current program.
    Last edited by 7stud; 04-21-2005 at 08:26 PM.

  12. #12
    Registered User
    Join Date
    Apr 2005
    Posts
    26
    I have done this. When I compile, it tells me


    [Linker error] undefined reference to `someFunc()'

    I'm still checking everything over right now.

  13. #13
    Resident nerd elnerdo's Avatar
    Join Date
    Apr 2005
    Location
    Northern NJ
    Posts
    51
    I'm confused as to why you have a seperate header file for all these things. Why not use classes?

  14. #14
    Registered User
    Join Date
    Apr 2005
    Posts
    26
    I don't know, you tell me. I am, to be quite honest, diving in head first to a dark hole. I don't know why not use classes. Would a class work for many differen't other files?

  15. #15
    Resident nerd elnerdo's Avatar
    Join Date
    Apr 2005
    Location
    Northern NJ
    Posts
    51
    I don't know either.

    I didn't even know you could make your own header files.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 02:59 AM
  2. C Pointers Problems
    By mhelal in forum C Programming
    Replies: 8
    Last Post: 01-10-2007, 06:35 AM
  3. String Manipulation problems -_-
    By Astra in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 05:48 PM
  4. Rendering problems (DirectX?)
    By OnionKnight in forum Tech Board
    Replies: 0
    Last Post: 08-17-2006, 12:17 PM
  5. contest problems on my site
    By DavidP in forum Contests Board
    Replies: 4
    Last Post: 01-10-2004, 09:19 PM