Thread: C++ Funtion/Include Problems.

  1. #31
    Registered User
    Join Date
    Apr 2005
    Posts
    26
    Yeah, All the above works. monster.h already works. It's GetStat in statcheck.h and statcheck.cpp that doesn't work.

    Monster.cpp is calling in GetStat() from statcheck.cpp/h

    That's where it doesn't work. Everything else works fine. If I remove GetStat from the program, it compiles and runs great.

    EDIT: Also, On the left, I have it added to the project. It's all there. But in the compile log, I see no signs of statcheck.h or statcheck.cpp
    Last edited by Smeep; 04-24-2005 at 05:04 AM.
    I'm using Dev-C++ 4.9.8.0

  2. #32
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Helllloooo? Did you do this:
    I'm pretty sure it's mandatory that you have to include monster.h in monster.cpp
    The monster.cpp file you posted does not do that, and I tested it and it got rid of your error.

    This stuff is frustrating, eh?

    If we could see your files, we could spot the error in 10 seconds, but it's so darn laborious to post all the files and their names.

    I suggest you get rid of monster.cpp and monster.h and try to get statcheck.h and statcheck.cpp to work with main().

    I want to reiterate the #1 Rule of debugging: you fix any obvious errors FIRST. The compiler is even directing you to GetStat(), and as I posted before:
    So, in your function definition here:
    Code:
    int GetStat(intelligence)
    
    {
     
              return intelligence * 2;
            
    }
    you are missing one of the required elements.
    I'm betting you never fixed that even though the compiler told you there is something the matter with that function.

    In addition, what is this:
    Code:
    main()
    {
        ...
        ...
    
        int Orc();
    }
    I suggest you read that post about Using Functions several times. If you don't know the basics of how to call, define, and declare functions, how can you expect to get that all straight in multiple files?
    Last edited by 7stud; 04-24-2005 at 06:34 AM.

  3. #33
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    This compiled 2nd try:
    Code:
    //main.cpp
    
    #include <iostream>
    #include "monster.h"
    
    using namespace std;
    
    
    int main()
    {
    	
    	Orc();
    
    	return 0;
    }
    Code:
    //monster.h
    
    #ifndef __MONSTER_H__
    #define __MONSTER_H__
    
    int Orc();
    
    #endif //__MONSTER_H__
    Code:
    //monster.cpp
    
    #include "statcheck.h"
    
    int Orc()
    {
    	int intelligence = 100;
    
    	GetStat(intelligence);
    
    	return 10;
    }
    Code:
    //statcheck.h
    
    
    #ifndef __STATCHECK_H__
    #define __STATCHECK_H__
    
    int GetStat(int);
    
    #endif //__STATCHECK_H__
    Code:
    //statcheck.cpp
    
    #include "statcheck.h"
    
    int GetStat(int intel)
    {
    	return 100;
    }
    Last edited by 7stud; 04-24-2005 at 06:29 AM.

  4. #34
    Registered User
    Join Date
    Apr 2005
    Posts
    26
    Lol, I've been reading reading and re-reading this entire thread.

    I'm pretty sure I'm getting there. Let me try "explaining" it.

    In a .h file, you are basically letting the other files know, that this name e.g. functionX() will appear below, and it's nothing to worry about, because the defintion is in functionX.cpp

    yeah?

    So I define in functionX.cpp

    Code:
    #include "functionX"
    using namespace ctd;
    int functionX()
    
    {
    
    random code here
    
    }
    and then in the function.h file, i'd have
    Code:
    //functionx.h
    #ifndef _FUNCTIONX_H
    #define _FUNCTIONX_H
    int functionX();  // Function prototype
    #endif
    then in main.cpp

    I would have

    Code:
    int main
    {
    
    functionX()
    
    return 0;
    
    }
    Is that right?

    I'll attatch my files, so you can look at them.

    edit: By the way, I'm not expecting you to change them, but If you do, make comments in the code and let me know what you changed, or what I should change it too, or smack me because I did something stupid oh and yes, this is frustrating. three days on one little problem.
    I'm using Dev-C++ 4.9.8.0

  5. #35
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    #include "functionX"

    should be:

    #include "functionX.h"

    then in main.cpp

    I would have
    Code:
    int main
    {
    
    functionX()
    
    return 0;
    
    }
    Is that right?
    Nope. What happened to this line of reasoning:
    Let me try "explaining" it.

    In a .h file, you are basically letting the other files know, that this name e.g. functionX() will appear below, and it's nothing to worry about, because the definition is in functionX.cpp
    Let's say your are a compiler. You are merrily reading down main() and all of a sudden your reverie is interrupted by:

    functionX();

    What the heck you say??!! Where did this intruder come from??!! I was never warned about having visitors today?!! I quit!

    Just like declaring a variable, you have to declare your functions before calling them, and functionX() was never declared in main(). Rememberer an 'include' just inserts the contents of the file at the include statement. That's why you need to include functionX.h in main(): it declares the function before you call it, which alerts the compiler that the function name it will see mentioned down in main() is defined in another file, and not to worry about the missing function definition.

    There are three types of function statements:

    1) a declaration/prototype: int functionX(int num);

    2) a function call: functionX(20);

    3) a function definition:
    Code:
    int functionX(int num)
    {
         return num * 100;
    }
    The only place that the line of reasoning that says, "you must declare a function before calling it", breaks down is: why do you have to include functionX.h in functionX.cpp? After all functionX.cpp just defines the function--it doesn't call it. I'm not really sure about that, but the #1 rule is: functionX.h has to be included in functionX.cpp. Then, after you include functionX.h in functionX.cpp, you worry about what other header files need to be in functionX.cpp. If there are other function names that are defined in other .cpp files that are mentioned in functionX.cpp, then you have to include their header files too--that gets back to the rule that says, "you have to declare a function before calling it".
    Last edited by 7stud; 04-24-2005 at 07:34 AM.

  6. #36
    Registered User
    Join Date
    Apr 2005
    Posts
    26
    Stupidly, I knew that. An honest mistake.

    Right now, In statcheck.cpp, I've changed the function getstat() definition to

    Code:
    // statcheck.Cpp
    #include <cstdio>
    #include <cstdlib>
    #include <iostream>
    #include "monster.h"
    #include "statcheck.h"
    
    using namespace std;
    
    
    int GetStat(int intelligence)
    
    {
     
              return intelligence * 2;
            
    }
    Is that what I was missing?
    I'm using Dev-C++ 4.9.8.0

  7. #37
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    That was the only problem I could see with StatCheck.

    You still have that int Orc() problem in main().

  8. #38
    Registered User
    Join Date
    Apr 2005
    Posts
    26
    I've changed that to
    Code:
    Orc();
    That correct?

    Man I love coding. I made the biggest thread on the page.
    I'm using Dev-C++ 4.9.8.0

  9. #39
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    lol. Is it FINALLY working???

  10. #40
    Registered User
    Join Date
    Apr 2005
    Posts
    26
    Nope

    Still not recognising GetStat()

    I'm guessing a problem with Dev-C++
    I'm using Dev-C++ 4.9.8.0

  11. #41
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    You can always test that by taking the files I posted and copying them into a new project, and seeing if it compiles. Create the project, then create each file, and copy and paste from above. It shouldn't take long.

  12. #42
    Registered User
    Join Date
    Apr 2005
    Posts
    26
    Roger wilco. Will report back with results.
    I'm using Dev-C++ 4.9.8.0

  13. #43
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    About the files you posted: I don't download files from the internet from anonymous sources, and the one time I made an exception to that rule was when someone posted an answer to a Visual C++ question I had that I really wanted to see, and my compiler was completely messed up for weeks after that....so I won't be able to see them unless you post them in code tags.

  14. #44
    Registered User
    Join Date
    Apr 2005
    Posts
    26
    That works. (no result as such, just runs and closes straight away)

    What I'm going to do, is get all my code, then remake the project. Just like I did with yours. Maybe Something went odd along the way.
    I'm using Dev-C++ 4.9.8.0

  15. #45
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Start off by creating a main.cpp, statcheck.h, and statcheck.cpp. Get those to compile before adding monster.h and monster.cpp.

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