-
Weird error in class..?
Hi, Im writing my own little version of that old game dope wars (anyone remember this?) Anyway, I get an error for each of the members of the class drugs. The error is as follows:
dope.cpp:188: `mushrooms' undeclared (first use this function)
dope.cpp:189: `dmt' undeclared (first use this function)
Only it does this for each member in the class. All 30 of them.
Heres the code for my game
Code:
#include <iostream.h>
#include <string.h>
#include <stdlib.h>
void setDef();
void drugsSetStatusT();
void drugsSetStatusF();
void menu();
int mainMenu(char menuString[40]);
void list();
class drugs
{
public:
char getName();
int setName(char tmp[]);
void setStatusT();
void setStatusF();
private:
char name[40];
bool status;
};
void drugs::setStatusT()
{
status = true;
}
void drugs::setStatusF()
{
status = false;
}
char drugs::getName()
{
cout << name;
return 0;
}
int drugs::setName(char tmp[])
{
strcpy(name, tmp);
return 0;
}
void main()
{
char sString[40];
setDef();
cout << "DOPE WARS LIN" << endl;
cout << "\nFor help type <HELP> \n";
cin >> sString;
mainMenu(sString);
}
int mainMenu(char menuString[40])
{
if (strcasecmp(menuString, "HELP")==0)
{
menu();
}
if (strcasecmp(menuString, "LIST")==0)
{
cout << endl;
}
return 0;
}
void menu()
{
system("clear");
cout << "COMMANDS:\n";
cout << "\nABOUT -Shows a brief description of the program and its author";
cout << "\nLIST -Shows items for sale";
cout << "\nBUY <item> -Buys the item";
cout << "\nSELL <item> -Sells the item";
cout << "\nEXAM <item> -Examines the item";
cout << "\nNEWS -Asks the locals for any drug news";
}
void list()
{
}
void setDef()
{
/* Hallucinogens */
drugs marijuana;
marijuana.setName("Marijuana");
drugs hash;
hash.setName("Hash");
drugs acid;
acid.setName("Acid");
drugs extacy;
extacy.setName("Extacy");
drugs mushrooms;
mushrooms.setName("Mushrooms");
drugs dmt;
dmt.setName("DMT");
drugs mescaline;
mescaline.setName("Mescaline");
/* Research Chemicals */
drugs amt;
amt.setName("AMT");
drugs dpt;
dpt.setName("DPT");
drugs stp;
stp.setName("STP");
drugs twocb;
twocb.setName("2-CB");
drugs twoct7;
twoct7.setName("2-C-T7");
/* Opiates */
drugs heroin;
heroin.setName("Heroin");
drugs opium;
opium.setName("Opium");
drugs oxyContin;
oxyContin.setName("OxyContin");
drugs vicodin;
vicodin.setName("Vicodin");
drugs fentanyl;
fentanyl.setName("Fentanyl");
drugs morphine;
morphine.setName("Morphine");
/* Dissasociatives */
drugs pcp;
pcp.setName("Angel dust");
drugs dxm;
dxm.setName("DXM");
drugs ketamine;
ketamine.setName("Ketamine");
/* Uppers */
drugs meth;
meth.setName("Meth");
drugs crack;
crack.setName("Crack");
drugs amphetamine;
amphetamine.setName("Amphetamine");
drugs cocain;
cocain.setName("Cocain");
drugs ice;
ice.setName("Ice");
/* Downers */
drugs ghb;
ghb.setName("GHB");
drugs xanax;
xanax.setName("Xanax");
drugs butisol;
butisol.setName("Butisol");
}
void drugsSetStatusF()
{
marijuana.setStatusF();
hash.setStatusF();
acid.setStatusF();
extacy.setStatusF();
mushrooms.setStatusF();
dmt.setStatusF();
mescaline.setStatusF();
amt.setStatusF();
dpt.setStatusF();
stp.setStatusF();
twocb.setStatusF();
twoct7.setStatusF();
heroin.setStatusF();
opium.setStatusF();
oxyContin.setStatusF();
vicodin.setStatusF();
fentanyl.setStatusF();
morphine.setStatusF();
pcp.setStatusF();
dxm.setStatusF();
ketamine.setStatusF();
meth.setStatusF();
crack.setStatusF();
amphetamine.setStatusF();
cocain.setStatusF();
ice.setStatusF();
ghb.setStatusF();
xanax.setStatusF();
butisol.setStatusF();
}
void drugsSetStatusT()
{
int marijuanaI = rand()%1000;
if (marijuanaI > 200)
{
marijuana.setStatusT();
}
int hashI = rand()%1000;
if (hashI > 800)
{
hash.setStatusT();
}
int xanaxI = rand()%1000;
if (xanaxI > 500)
{
xanax.setStatusT();
}
}
Hmm thats pretty long, sorry. I really have no clue why this isnt working, any help would be GREATLY appreciated. Thank you :)
-
hmmm
I also use g++ in linux, do you think it might be something wrong with the compiler... probably not but maybe.
Thanks again for anyone that helps
-
Looks like it is a matter of scope. The only place you are ever creating any of the drugs class objects is within the setDef function, these objects will all be destroyed once the function exits. In the drugsSetStatusF you are trying to call member functions for objects that aren't visible within the scope of the function. You could move all of your drugs marijuana;, drugs heroin;, etc. declarations out of the setDef function and place them all above main so they become globals. Otherwise you will have to end up passing all 30 some objects via function parameters to each and every function that does something with those objects.
-
aww thanks