Thread: Hard to get classes

  1. #16
    Registered User
    Join Date
    Nov 2001
    Posts
    53

    Unresolved blah

    Try declaring that array in your source file instead of the header file.

    Almosthere
    "Technology has merely provided us with a more efficient means for going backwards" - Aldous Huxley

  2. #17
    Registered User kitten's Avatar
    Join Date
    Aug 2001
    Posts
    109
    Winterwolf>
    nononononoo! No code in header!

    Put your class definition in header:

    class Monster
    {
    // whatever
    };

    // then the array in .cpp file:

    const int size = 5;
    Monster monsterArray[size];

    // and then in the header file again:

    extern const int size;
    extern Monster monsterArray[];

    Hope this helps
    Making error is human, but for messing things thoroughly it takes a computer

  3. #18
    Registered User
    Join Date
    Nov 2001
    Posts
    32

    Unhappy Hmm no, something still...

    It doesent work, maybe it has something to do ith how I give each object itīs values.

    In the .cpp file I do like this:

    ex.
    monsterArray[0].name("Brainleach");
    and so on, up to MonsterArray[4], how would you do when you give the monsterArray itīs values?

    Can I give the array itīs values in the Monster.cpp file
    lets say that in my defined class I will define a method called createMonsters(), or cant I use a class method to give the values to the array. Do I have to use a non member function to do this?

  4. #19
    Registered User
    Join Date
    Nov 2001
    Posts
    32

    I really donīt know..

    I have tested some more, and I made a temp.cpp file

    I included the Monster.h file, and added the Monster.cpp file to the project.
    In the temp.cpp file I made a function called createMonster(), in testing purpose, and I gave the array of objects the values I wanted it to have, but the compiler still complains. it is the MonsterArray[] that has something to do with this I think, I really hopes that someone knows what Im doing wrong here!!

    This is how I have done in the test:

    #include "Monster.h"

    void createMonster()
    {
    const int size = 5;
    Monster monsterArray[size];

    monsterArray[0].setName("DirtBeast");
    monsterArray[0].setArmor(10);
    monsterArray[0].setDamage(9);
    monsterArray[0].setLife(10);
    monsterArray[0].setExpGive(40);

    monsterArray[1].setName("BrainLeach");
    monsterArray[1].setArmor(8);
    monsterArray[1].setDamage(11);
    monsterArray[1].setLife(5);
    monsterArray[1].setExpGive(50);

    monsterArray[2].setName("shadow-Wolf");
    monsterArray[2].setArmor(10);
    monsterArray[2].setDamage(9);
    monsterArray[2].setLife(10);
    monsterArray[2].setExpGive(65);

    monsterArray[3].setName("Dracoon");
    monsterArray[3].setArmor(12);
    monsterArray[3].setDamage(10);
    monsterArray[3].setLife(9);
    monsterArray[3].setExpGive(80);

    monsterArray[4].setName("Mist-Creature");
    monsterArray[4].setArmor(11);
    monsterArray[4].setDamage(11);
    monsterArray[4].setLife(12);
    monsterArray[4].setExpGive(100);
    }

    int main()
    {
    createMonster();
    return 0;
    }

    I must be doing something awfully wrong..

  5. #20
    Registered User
    Join Date
    Nov 2001
    Posts
    53

    Hmm

    Ok, i think the problem may be in your monster source file (or the header). This is the way i'd do it (may be flawed and without all of your variables) :

    In the main souce file :

    #include <iostream.h>
    #include "MonsterClass.h"

    void main(void) {
    MonsterClass MyMonsters[10];
    MyMonsters[0].setname("Goggle Eyes");

    cout << MyMonsters[0].getname() << endl;

    }

    In the monster header file :

    class MonsterClass {

    protected :
    char *MyName;


    public :
    const char *getname();
    void setname(const char *Str);

    MonsterClass() {
    MyName = new char[];
    }

    };

    In the monster source file :

    #include "MonsterClass.h"

    const char *MonsterClass::getname() {
    return(MyName);
    }

    void MonsterClass::setname(const char *Str) {
    strcpy(MyName,Str);
    }

    Compare this way with your own code and see if you can see any major differences ... I think you may do Hope it helps..

    Almosthere

    PS - you may wanna use a constructor for your monster class 'cause all that setting and getting could get tedious.
    "Technology has merely provided us with a more efficient means for going backwards" - Aldous Huxley

  6. #21
    Registered User
    Join Date
    Nov 2001
    Posts
    32

    Thanx, but...

    Here I will print the header, but all the variables and methods should be ok, for if I dont use the array of objects it compiles without a problem..

    class Monster
    {
    public:
    Monster();
    ~Monster();

    // set functions
    void setName(char n[]); voidsetDamage(int d);
    void setArmor(int a);
    void setLife(int l);
    void setExpGive(int exp);

    // get functions
    const char* getName()const;
    int getDamage()const;
    int getArmor()const;
    int getLife()const;
    int getExpGive()const;

    private:
    char name[30];
    int damage,armor,life;
    int expGive;
    };

  7. #22
    Registered User
    Join Date
    Nov 2001
    Posts
    32

    Ops..

    void setDamage(int d);
    Jumped wrong when I printed it here!!! and it lost a space, not in the real code though..

  8. #23
    Registered User
    Join Date
    Nov 2001
    Posts
    53

    Lightbulb Ok...

    ... I tell you what - mail me your code (with the source for the header file too) and I'll fix it. How does that sound?

    [email protected]


    Almosthere
    "Technology has merely provided us with a more efficient means for going backwards" - Aldous Huxley

  9. #24
    Registered User
    Join Date
    Nov 2001
    Posts
    32

    Re: Ok...

    Originally posted by almost_here
    ... I tell you what - mail me your code (with the source for the header file too) and I'll fix it. How does that sound?

    [email protected]


    Almosthere
    Im really tankful about your offer, but I fixed it, thanx for all the information though.

    It was the Constructor, and Destructor that was wrong...
    Donīt know why they hade to be:
    Monster(){}
    ~Monster(){}
    Instead of:
    Monster();
    ~Monster();

    I hope that people will still continue good stuff about classes here though, always good to have all information in one place, and I will write here if I get any more problems...(and i know that I will)

  10. #25
    Registered User
    Join Date
    Nov 2001
    Posts
    53

    Hmm

    What compiler are you using?

    I've never heard of that one before. In fact i'd probably go so far to say it is not the cause of your problem and that you are likely to be leaking somewhere along the line (thoughts anyone else?).. in which case you'll be back here sooner than you think!



    Almosthere
    "Technology has merely provided us with a more efficient means for going backwards" - Aldous Huxley

  11. #26
    Unregistered
    Guest
    Code:
    //Monster.h
    
    class Monster 
    { 
    public: 
    Monster(); 
    ~Monster(); 
    
    // set functions 
    void setName(char n[]); 
    void setDamage(int d); 
    void setArmor(int a); 
    void setLife(int l); 
    void setExpGive(int exp); 
    
    // get functions 
    const char* getName()const; 
    int getDamage()const; 
    int getArmor()const; 
    int getLife()const; 
    int getExpGive()const; 
    
    private: 
    char name[30]; 
    int damage,armor,life; 
    int expGive; 
    };
    
    /////////////////////////////////////////////////////////////////////////
    
    //Monster.cpp
    
    #include "Monster.h"
    #include <string.h>
    
    //default monster does not intialize any of the private member 
    //variables in this code.  Intializing will be done via mutators.
    Monster::Monster() 
    {}
    
    //default destructor is acceptable since no dynamic memory 
    //used.  Can write it if you wish.
    Monster::~Monster()
    {}
    
    //default assignment operator and copy constructor acceptable
    //and therefore not written
    
    //define mutator and accessor functions here
    void Monster::setName(char n[])
    {
    stcpy(name, n);
    }
    
    void Monster::setDamage(int d)
    {
    damage = d;
    }
    
    //etc.
    
    }
    //end Monster.cpp
    
    /////////////////////////////////////////////////////////////////
    
    //Game.cpp
    
    #include <iostream.h>
    #include "Monster.h"
    
    int main()
    {
    const int SIZE = 5;
    char _name[30];
    int _damage;
    //other variables as necessary, etc.
    
    //this declares array of 5  Monsters using default constructor
    Monster Monsters[SIZE];
    
    //now initialize data for each Monster in Monsters[] since default  
    //constructor didn't do it for you
    int i;
    for(i = 0; i < SIZE; i++)
    {
    cout << "enter name for first type of monster" << endl;
    cin >> _name;
    Monsters[i].setName(_name);
    cout << "enter damage value" << endl;
    cin << _damage;
    Monsters[i].setDamage(_damage);
    //etc.
    }
    
    //display each Monster in Monsters[]
    for(i = 0; i < SIZE; i++)
    {
    cout << Monsters[i].getName() << endl;
    cout << Monsters[i].getDamage() << endl;
    //etc.
    }
    
    return 0;
    }
    If you don't want to see the code to initialize Monsters[] in main()you can create a second header file called Combatants and declare and intialize Monsters[] in there rather than in main() .
    If you do this then you need to #include "Combatants.h"
    to Game.cpp and remove the duplicate code from Game.cpp.

    You can hardcode the data in Monsters[] wherever you declare and initialize Monsters[] if your prefer, rather than giving user opportunity to do so as in the code above.

    Note: should use inclusion gaurd when writing header files. Left out here for sake of clarity.

  12. #27
    Registered User
    Join Date
    Nov 2001
    Posts
    32

    Smile I really apriciate..

    Nice of you taking the time to write code and explaining it to me Thanx, I have another problem though with my array of objects.

    ex.
    const int size = 5;
    Monster monsterArray[sise];

    I declare a array of my monster class, after that I give values to each of the objects..

    monsterArray[0].name("BrainLeach");
    monsterArray[0].damage(10);
    monsterArray[0].armor(20);
    monsterArray[0].life(12);
    And so on until I react the [4] element

    Ok now I have given each element in the array values, but I dont want to use them in that method/function. What should I do if I want to use THAT array of objects in another function, with the values that I have assigned to it???

  13. #28
    Registered User
    Join Date
    Nov 2001
    Posts
    53
    Not quite sure what you mean... could you explain further? Do you want to use the array in another class, a function in the same class or somewhere else entirely?

    A.h.
    "Technology has merely provided us with a more efficient means for going backwards" - Aldous Huxley

  14. #29
    Registered User kitten's Avatar
    Join Date
    Aug 2001
    Posts
    109
    Winterwolf:

    If you want to use an array in a function where it isn't visible, you must give a pointer to the array for the function. Then you can use the entire array via pointer. The pointer points to the begin of the array in memory. I.e. the pointer contains the direct memory access to that location where is the array's first object's first byte.
    Code:
    void MyArrayFunction(Monster* MonsterPtr)
    {
      MonsterPtr->GetFunc();  // GetFunc() of the first object in array
      (MonsterPtr+1)->GetFunc();  // GetFunc() of the second object in array
      (MonsterPtr+69)->SetFunc();  // SetFunc() of the 70th object in array
    Of course, the class must be known in the .cpp file where the function is. If it isn't then you need to #include the classes header file.

    Happy coding
    Making error is human, but for messing things thoroughly it takes a computer

  15. #30
    Registered User
    Join Date
    Nov 2001
    Posts
    32

    Unhappy Hmm...

    Sorry, for I didnt really understand what you meant. I will try to explain what I want to do again, and maybe you could help me with it.

    I have a function:
    void Monster::createMonster()
    {
    // Here I make an Array of objects, of this class
    const int size = 5;
    Monster monsterArray[size];

    // I give each element of the array an value
    monsterArray[0].name("Dracoon");
    monsterArray[0].damage(12);
    monsterArray[0].armor(20);
    monsterArray[0].life(14);
    // And so on until I reach the fifth element, wich is 4

    }

    The problem is that I only want to create the monsters here, but use them in another class and function...

    Maybe you described just this Kitten, but I didnt really get it. Did you mean that I have to have a return value, that is a pointer to a Monster object?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-06-2005, 07:11 PM
  2. Abstract classes
    By cisokay in forum C++ Programming
    Replies: 17
    Last Post: 05-29-2005, 09:39 AM
  3. Trinary Hard Drive
    By nickname_changed in forum Tech Board
    Replies: 14
    Last Post: 05-13-2005, 10:01 AM
  4. Problems with memory allocation in classes
    By MathFan in forum C++ Programming
    Replies: 6
    Last Post: 01-09-2005, 02:05 AM
  5. Sharing a variable between classes of different .CPP files
    By divingcrab in forum C++ Programming
    Replies: 5
    Last Post: 07-07-2002, 02:57 PM