call works in constructor, not in void function

The below code is pulled from a chassis i'm building to learn class interactions. The constructor works great.. the shop is created, the item bank is instanced and the shops wares are populated from the item bank. Later when i create a function to clear and refill the inventory, i get red squiglies.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//constructor
store::store(string lname)
{
	name = lname;
	items items(store::get_name());
	cout << "A store named " << name << " has been created" << endl << endl;
	for(int i = 0; i < 3; i++)
	{
	wares.push_back(items.hand_item());				//pulls random items from items class
	}
	wares.push_back(items.create_item(rand()%5));		//pulls item from items class random item generator
	
}
void store::load_store()
{
	wares.empty();
	for(int i = 0; i < 3; i++)
	{
		wares.push_back(items.hand_item());
	}
	wares.push_back(items.create_item(rand()%5));
}

Both 'items.' give an error, "a non static member reference must be relative to an object".
The items object is created in your constructor. It goes out of scope disappears when the constructor is finished doing its job.
closed account (zb0S216C)
Line 5 is suspect. You need to change the name of the "items" instance to something else because you're confusing the compiler. For future reference, storage identifiers cannot have the same name as either reserved words ("return", "int", "void", etcetera) or identifiers of classes.

Wazzak
Thanks, seems obvious now.

can i call it as static? I tried sticking static in front of it and still had errors. What should the code look like?
static is not what you want.

The current problem is that the 'items' object is local to the constructor.. since that is where you're creating it. This means that as soon as the constructor exits, the 'items' object goes away.

When you try to use the 'items' object in your load_store function it doesn't exist (it was not created in that function). Since there is no 'items' object, the compiler things you're referring to the class with the same name which makes it spit out the weird error because what you're doing makes no sense if 'items' is a class name.

So like Framework said.. give that object a different name. If the class is named 'items', then name the object something else.

But that alone will not solve the problem. The problem still is that the 'items' object does not exist in that other function. There are two solutions for this:

1) Create another 'items' object in your load_store function.

or

2) Increase the lifetime/scope of the other 'items' object so it doesn't go out of scope and die at the end of the constructor. This can be done by making it a member of the 'store' class and no longer creating it in the constructor.
Thanks Disch, great answer. I'll research the syntax of member objects.
Alright, reopening this. I haven't been able to find a good example of how to fix it. Here is the relevant part of the class delcaration.

1
2
3
4
5
6
7
class store
{
private:
	string name;
	vector<string> wares;
	vector<int>::iterator iter;
	items itembank(string name);


Am i not properly making itembank a member or am i not calling it properly in load_store? The error i get is a red squigly under itembank and the warning "expression must have class type". The goal is the itembank object to be a persistant part of the store object.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//constructor
store::store(string lname)
{
	name = lname;
	items items(store::get_name());
	cout << "A store named " << name << " has been created" << endl << endl;
	for(int i = 0; i < 3; i++)
	{
	wares.push_back(itembank.hand_item());				//pulls random items from items class
	}
	wares.push_back(items.create_item(rand()%5));		//pulls item from items class random item generator
	
}
void store::load_store()
{
	wares.empty();
	for(int i = 0; i < 3; i++)
	{
		wares.push_back(itembank.hand_item());
	}
	}


This

items itembank(string name);

is a function declaration. So what does mean this statement?


wares.push_back(itembank.hand_item());

you would think i would have stumbled upon that randomly deleting and adding bits as much as i did. I know at some point i declared it as items itembank; and it didn't work.. but something else must have been broken at that point.

Thanks again.
Topic archived. No new replies allowed.