From what I understand, passing an entire structure to a function and not doing so by reference can be taxing on system resources. So, I’d like to get a handle on passing them by reference. I’m creating a character generator with the following structures:
struct Stats{
string name;
int level;
int HP;
int STR;
int CON;
int DEX;
int INT;
int WIS;
int CHA;};
struct Growth{
int HPperlvl;
int STRperlvl;
int CONperlvl;
int DEXperlvl;
int INTperlvl;
int WISperlvl;
int CHAperlvl;};
struct Holdstats{
Stats classstats;
Growth classgrowth;};
const int SIZE = 10;
Holdstats classlist[SIZE] = {
{ {"Fighter", 1, 18, 10, 10, 10, 10, 10, 10}, {1,1,1,1,1,1,1} },
{ {"Wizard", 1, 10, 10, 10, 10, 10, 10}, {1,1,1,1,1,1,1}},
{ {"Rogue", 1, 10, 10, 10, 10, 10, 10}, {1,1,1,1,1,1,1}}
} //These are just some examples, will be filled later
Holdstats charlist[SIZE]; //Empty array to store created characters
I’ll use the simplest function in the program to illustrate my problem. The following function is simply supposed to display info for whatever structure is passed to it, at whatever position in the structure is passed to it. The position is previously defined in the program. I would like to pass both the struct and the position by reference.
void dispinfo(const Holdstats &p, int &i) //Should be passed a position and a structure
{
cout << endl << "\tHere is the Character/Class info you requested: "
<< "\n\t----------------------------------------------"
<< "\nName:\t\t" << p[i].classstats.name << endl
<< "Level:\t\t" << p[i].classstats.level << endl
<< "Hit Points:\t\t" << p[i].classstats.HP << endl
<< "Strength:\t\t" << p[i].classstats.STR << endl
<< "Constitution\t\t" << p[i].classstats.CON << endl
<< "Dexterity\t\t" << p[i].classstats.DEX << endl
<< "Intelligence\t\t" << p[i].classstats.INT << endl
<< "Wisdom\t\t" << p[i].classstats.WIS << endl
<< "Charisma\t\t" << p[i].classstats.CHA << endl;
}
The issue I’m getting has to do with p[i]. My compiler is telling me that for
Holdstats &p – no operator [] matches these operands. I would just use the name of a previously defined structure array (“classlist” or something) in the function header, but I want to be able to pass any array of structures to the function.
To my knowledge, I’m calling element i of a previously defined array. But obviously, I’m doing something wrong. Can anyone see the problem?
A few thoughts to try and help out.
iwithindispinfo(), there’s really no reason to pass it by reference. Passing anintand passing a reference to anintis probably equivalent (both 32-bits more than likely) but then accessing thatintby reference is (trivially) more taxing since it has to be dereferenced to get at the value.Holdstatsstructs to get passed intodispinfo(). If that’s the case, you don’t need the reference symbol as arrays are always passed by reference. You should useHoldstats p[]instead ofconst Holdstats &pand the array would get passed in just fine (and you could compile). To calldispinfo()you’d just pass the array name and the index you want to print out.You may want to consider changing
dispinfo()to only receive a singleHoldstatsstructrather than an array of them. Then the function would look something like this.Then your call to
dispinfo()would look something like this.That’s what you’d effectively be doing by passing the array and an index like so:
dispinfo(classlist,0);