Weird error in class..?

This is a discussion on Weird error in class..? within the C++ Programming forums, part of the General Programming Boards category; Hi, Im writing my own little version of that old game dope wars (anyone remember this?) Anyway, I get an ...

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    5

    Unhappy 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

  2. #2
    Registered User
    Join Date
    Jul 2002
    Posts
    5

    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

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    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.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Jul 2002
    Posts
    5

    Thumbs up aww thanks

    Thanks, you were right.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating a database
    By Shamino in forum Game Programming
    Replies: 19
    Last Post: 06-10-2007, 01:09 PM
  2. Mmk, I give up, lets try your way. (Resource Management)
    By Shamino in forum Game Programming
    Replies: 31
    Last Post: 01-18-2006, 08:54 AM
  3. class errors
    By romeoz in forum C++ Programming
    Replies: 3
    Last Post: 09-16-2003, 07:57 PM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 06:18 AM
  5. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 01:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21