class member function's object not in scope?

how do you call a member function from a member function? What am i missing? it could just be that it is 6AM lol

im trying to call medkit.stat() from store::storeMenu()
when i compile i get medkit out of scope in the member function

class
1
2
3
4
5
6
7
8
9
10
class store
{
public:
    string name;
    string desc; //decription
    int price;
    void stat(); // display item information
    void storeMenu(); //display menu of items and options
    store();
};

member function
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
void store::storeMenu()
{
    char choice;//needs to be char for choices cuz of '1'

    //display money amount
    cout << "**************" <<endl;
    cout << "  Money $" << MONEY <<endl;
    cout << "**************" <<endl <<endl;


    cout << "Choose which supply you would like to purchase" << endl;
    cout << "E) ***Exit Menu***"  <<endl<<endl;

    cout << "1) Medkit" << endl;
    cout << "2) Lighter" << endl;
    cout << "3) Knife" << endl;
    cout << "4) Food" <<endl;
    cout << "5) Weapons" << endl;
    cout << "6) Clothing" << endl;
    cout << "7) Rope" << endl;
    cout << "8) Tent" <<endl;
    cout << "9) Compass" << endl<<endl;

    cin >> choice;


        if(choice == '1')//choice for items
                {
                    medkit.stat();
                }
......}

main()
1
2
3
4
5
6
7
int main()
{
    store medkit;
    medkit.name = "Medical Kit";
    medkit.price = 100;
    medkit.desc = "A medkit will heal your party's wounds. ";
......}


in addition to my original question, i was wondering about all the data members and member functions of the class store? they fill a bit of main, Is there any way to get some of that code out of main???
Last edited on
If medkit has been declared in main(), storeMenu() has no knowledge of it. You should either make madkit a member of store or pass it to storeMenu(). Note that logically medkit is not a store, it's an item. You need to write an item class and give store a list (array or vector) of those.

For your other question, look into constructors.
I see a lot of other problems with this. To access it using the identifier would require it to be a static member. However, this is terrible practice. You might want to store it (like the above) in a list, map or a vector array. Then you could loop through and allow it to be accessed using a const/enum or string.

Also consider making a constructor. Such as:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <map>
#include <string>

using namespace std;

class Store
{
    static void PrintMenu(); //display menu of items and options
};

class Item
{
public:
    static map<string, Item*> Items;

    const string desc; //decription
    const int price;

    Item(string name, string desc = "", int price = 0):
        desc(desc), price(price)
    {
       Items[name] = this;
    }
};

map<string, Item*> Item::Items;
int main()
{
    new Item("Pie", "Yummy", 100);
    cout << Item::Items["Pie"]->desc;

    return 0;
}
Last edited on
closed account (zb0S216C)
Turbine wrote:
 
new Item("Pie", "Yummy", 100);
(sic)

Who owns this resource block?

Wazzak
Topic archived. No new replies allowed.