Thread: #Include "class" problems

  1. #1
    Unregistered
    Guest

    Unhappy #Include "class" problems

    Hello to you all!

    I have I problem with a project of mine, it occures when I want to include a class that I have made...

    Looks something like this
    //Class to include
    Class Adventure
    {
    public:
    void happenOnRoll();
    void monsterMeeting();
    void findItem();
    void findGold();
    private:
    }a;
    // end of class definition

    I donīt have to have this in a class, but I placed it there to save some room in the main file..

    Ok! now when I include this file to my main.cpp file, everything is ok. But when I call for a function ex. a.happenOnRoll(), the compiler starts to complain...
    It says something like this:
    void __thiscall Adventure::happenOnRoll(void)

    I really hope that someone can help me on this, Thanx

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Class Adventure
    {
    public:
    void happenOnRoll();
    void monsterMeeting();
    void findItem();
    void findGold();
    private:
    }a;


    Dont declare an instance in your declaration......

  3. #3
    Unregistered
    Guest

    That wont work

    Thanx for trying to help, but I have tried that and it doesent make a different if I do or not..
    This is the .cpp file to the above header file, if it helps

    #include <iostream>
    #include <ctime>
    using namespace std;
    #include "Adventure.h"

    void Adventure::happenOnRoll()
    {
    int rndSeed = 4;
    int randAction;

    randAction = random(rndSeed);

    switch (randAction)
    {
    case 1:
    monsterMeeting();
    break;

    case 2:
    itemFind();
    break;

    case 3:
    goldFind();
    break;
    }
    }

    // Function that randomizes a number between 1 and RND_MAX
    int Adventure::random(int RND_MAX)
    {
    unsigned int rndNum;
    rndNum = 1 + rand() % RND_MAX;
    return rndNum;
    }

    void Adventure::monsterMeeting()
    {
    // just putting out text for the moment
    cout << "You see a horrid creature coming at you" << endl;
    }

    void Adventure::itemFind()
    {
    // just putting out text for the moment
    cout << "You have found an item" << endl;
    }

    void Adventure::goldFind()
    {
    // just putting out text for the moment
    cout << "You have found some gold" << endl;
    }

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    10
    Here's what you need to do:

    in the header file...

    Code:
    #ifndef ADVENTURE_H
    #define ADVENTURE_H
    
    //Class to include 
    Class Adventure 
    { 
    public: 
    void happenOnRoll(); 
    void monsterMeeting(); 
    void findItem(); 
    void findGold(); 
    private: 
    }; 
    // end of class definition 
    
    #endif //ADVENTURE_H
    the preprocessor stuff prevents the header being included more than once.

    Now put the actual class methods in a separate file, which should include the above header.

    Now include the header in all files which used this class, and you should be ok.

    hope this helps,
    rob
    www.goldroad.co.uk
    freeware ARM assembler
    dynarec gameboy advance emulator
    try them now!

  5. #5
    Unregistered
    Guest

    Unhappy My methods is in a separete file, the above .cpp file

    Hmm maybe I missled you by not printing both the .h and the .cpp file directly after one nother, but I include a file named Adventure.h, to a file named Adventure.cpp, where I have my methods.

    And sorry about that but I forgot to write that I use as u did write.
    #ifndef ADVENTURE_H
    #define ADVENTURE_H

    "class definition"

    #endif

  6. #6
    Registered User
    Join Date
    Sep 2001
    Posts
    10
    ok, i think it might just be that you've written "Class" and not "class"?? it should be lower case (upper case in java if i remember correctly...)
    www.goldroad.co.uk
    freeware ARM assembler
    dynarec gameboy advance emulator
    try them now!

  7. #7
    Unregistered
    Guest

    Smile Hmm, this could take a wile, if I keep...

    I am really sorry about that, I accidentally wrote a C instead of c in the message.
    The strange thing is that I have another class that I include to the main file, and it works as it should, so I dont really understand why I cant get this to work...

    This is my other header file that works after being included:

    #ifndef CHARACTER_H
    #define CHARACTER_H

    const int CH_MAX = 30;
    class Character
    {
    public:
    Character()
    {
    x = 5;y = 5;
    strcpy(name,"Unnamed");
    strcpy(gender,"Unknown");
    }
    ~Character(){}
    // set Functions
    void setXcoord(int xc){ x = xc; }
    void setYcoord(int yc){ y = yc; }

    // get Functions
    int getXcoord(){ return x; }
    int getYcoord(){ return y; }

    // other
    void walking();

    char name[CH_MAX];
    char gender[CH_MAX];
    private:
    int x,y;

    }c;

    #endif

  8. #8
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    why do you want to use a class when these functions have no common data to operate on. A more sensible idea maybe is to wrap those functions up in a namespace instead.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  9. #9
    Unregistered
    Guest

    Smile

    Originally posted by Stoned_Coder
    why do you want to use a class when these functions have no common data to operate on. A more sensible idea maybe is to wrap those functions up in a namespace instead.
    Maybe I should, though im not that familiar with namespaces. May be you no of a good place to learn about it Thanx.

    But that wasent my question I know that it isnt good to have those functions in a class, but it should work anyway, and I wanted to know why I cant use the header file after I have included it...

  10. #10
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    maybe you needed to extern your a object.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  11. #11
    Unregistered
    Guest

    Unhappy

    Originally posted by Stoned_Coder
    maybe you needed to extern your a object.
    I have tried it, but it doesent help. I will have to try and solve this myselfe it looks like, but if you do find out what the wrong is, I would highly apriciate if you print it here for me....
    Read all my messages to get some understanding of what I am doing, or what I want to do =)

  12. #12
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    what is the exact error.... copy and paste work well.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  13. #13
    Unregistered
    Guest

    Unhappy Here it is (unresolved external)

    unresolved external symbol "public: void __thiscall Adventure::happenOnRoll(void)

  14. #14
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    in your header file underneath the class dec. add this line and see if it helps...

    extern Adventure a;
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  15. #15
    Unregistered
    Guest

    Unhappy Sorry!

    Im really sorry to say but it doesent, but I know that it is something wrong with this header file, because I include another one to my main file, and that one works. But I cant see why the later one wont work.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help me with function call
    By NeMewSys in forum C++ Programming
    Replies: 16
    Last Post: 05-22-2008, 01:53 PM
  2. C programing doubt
    By sivasankari in forum C Programming
    Replies: 2
    Last Post: 04-29-2008, 09:19 AM
  3. some problems with #include
    By Cmaniac in forum C Programming
    Replies: 6
    Last Post: 04-15-2007, 11:28 PM
  4. Headers that use each other
    By nickname_changed in forum C++ Programming
    Replies: 7
    Last Post: 10-03-2003, 04:25 AM