Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

?
Lv 5

C++ Class (Help a brother out)?

I wrote a class in c++, and I have an array of data. I am trying to get the class to except the array, but am having a terrible time. The only way I can get it to work, is if I put the actual array in main function, of the main.cpp file. I want it try to keep the array in the monster.h file and then assign whatever monster I am using at the time, a more manageable variable name. So instead of

mon[53].name, or mon[69].desc, I would be able to use something like critter.name and critter.desc... Can someone give me a clue as to what I'm doing wrong here?

Monster.h

#ifndef __MONSTER_H__

#define __MONSTER_H__

#include <ctime>

#include <string>

#include <iostream>

class monster

{

public:

monster(){};

~monster(){};

string name;

int lvl;

int ac;

int gend;

int maxdam;

int gold;

int hits;

int exp;

string wep;

int wepdam;

string desc;

string getname(){return name;}

int getlvl(){return lvl;}

int getac(){return ac;}

int getgend(){return gend;}

int getmaxdam(){return maxdam;}

int getgold(){return gold;}

int gethits(){return hits;}

int getexp(){return exp;}

string getwep(){return wep;}

int getwepdam(){return wepdam;}

string getdesc(){return desc;}

};

class npc:public monster

{

public:

npc(string _name="", int _lvl=0, int _ac=0, int _gend=0, int _maxdam=0, int _gold=0, int _hits=0, int _exp=0, string _wep=0, int _wepdam="", string _desc="")

{name=_name;

lvl=_lvl;

ac=_ac;

gend=_gend;

maxdam=_maxdam;

gold=_gold;

hits=_hits;

exp=_exp;

wep=_wep;

wepdam=_wepdam;

desc=_desc;

}

};

#endif

The data array goes like this:

mon[0]=npc("name",1,0,0,5,10,6,5,"wep name",2,"mons description.");

mon[1]=npc("name",1,0,0,5,10,6,5,"wep name",2,"mons description.");

mon[2]=npc("name",1,0,0,5,10,6,5,"wep name",2,"mons description.");

mon[3]=npc("name",1,0,0,5,10,6,5,"wep name",2,"mons description.");

mon[4]=npc("name",1,0,0,5,10,6,5,"wep name",2,"mons description.");

and so on up to

mon[299]=npc("name",1,0,0,5,10,6,5,"we… name",2,"mons description.");

3 Answers

Relevance
  • 1 decade ago
    Favorite Answer

    There are a number of ways you could do this. Here's one solution that's fairly clean:

    Add these lines at the end of your monster class definition, between getdesc and the closing }:

    static monster &getMonsterbyid(int id);

    static monster &getMonsterbyname(string name);

    private:

    static monster monsters[];

    Add a monsters.cpp file to your project if you don't already have one.

    monsters.cpp:

    #include "monsters.h"

    monster monster::monsters[]

    =

    {

    npc("name1",1,0,0,5,10,6,5,"wep name",2, "mons description."),

    npc("name2",1,0,0,5,10,6,5,"wep name",2, "mons description."),

    npc("name3",1,0,0,5,10,6,5,"wep name",2, "mons description."),

    npc("name4",1,0,0,5,10,6,5,"wep name",2, "mons description."),

    npc("name5",1,0,0,5,10,6,5,"wep name",2, "mons description.")

    // etc

    };

    monster &monster::getMonsterbyid(int id)

    {

      return monsters[id];

    }

    monster &monster::getMonsterbyname(string name)

    {

        for(int i=0;i<sizeof(monsters)/ sizeof(monsters[0]);i++)

            if(monsters[i].name == name)

                return monsters[i];

    }

    Now, in main (or wherever) you can write:

    monster &critter = monster::getMonsterbyid(1);

    cout << critter.name << endl;

    critter = monster::getMonsterbyname("name4");

    cout << critter.name << endl;

    Here's what's going on:

    I made your monsters array a static member of monster. Static means there will only ever be 1 array, it's a global, more or less, but because it's private, the only way to get to it is through the monster class. The array is initialized in monsters.cpp using an array initializer list. To add more monsters, you just add to this list, you don't have to touch any other code.

    I also added 2 public static functions to monster: getMonsterbyname and getMonsterbyid.

    These return a *reference* to a monster, so you can the the dot syntax you want in main. (Note, I left out any sort of error checking here. If you pass an invalid name or id, really bad things will probably happen).

    HTH

  • 1 decade ago

    Those array initialization statements don't do anything unless they are included inside a method or function, and that method is called in a context where an array named "mon" actually exists. Since you want to include them inside of a function, and only in one place, then you probably don't want to have them in the same include file as the class declaration, which will be included in *every* source file that references or implements the class.

    Is there any reason that you can't have this in the form of a cpp source instead of a header file? If so (such as, the header file will be generated by another program rather than being typed in), then you could use a separate header file ("monsterinit.h") and then include within the function where you want the statements to be compiled.

    Like:

    static npc[] new_mon_array()

    {

    ... mon = new npc[300];

    ... #include "monsterinit.h"

    ... return mon;

    }

  • 1 decade ago

    try using associative arrays in c++:

    check out the map function.

Still have questions? Get your answers by asking now.