nonstatic member reference?

Im trying to access public data members from a base class. But i get an
Error: a nonstatic member reference must be relative to a specific object.

this is the class
1
2
3
4
5
6
7
8
9
10
  class SlotMachine
{
public:
	int m_creditpool;
	int m_rewardpool;
	SlotMachine();
	int play();
	void add_quarters( int quarters );
	int check_quarters() const;
};


here is a function from a derived class trying to acces those data members.
1
2
3
4
5
int PowerSlots::play()
{
	if( SlotMachine::m_creditpool > 0 )
	{
		if( lasttimewon < 5 )

any tips greatly appreciated!
I do not see any invalid construction in the function. Show how class PowerSlots is defined.
An object of type SlotMachine needs to be defined,
1
2
SlotMachine a;
if ( a.m_creditpool > 0 )

@Chervil

An object of type SlotMachine needs to be defined,

SlotMachine a;
if ( a.m_creditpool > 0 )


According to the original post class PowerSlots is derived class of SlotMachine. So m_creditpool would be a data member of the derived class.
Heres class PowerSlots
1
2
3
4
5
6
7
class PowerSlots : public SlotMachine
{
public:
	int play();
	int PowerPlay();
	PowerSlots();
};
As I said I do not see the reason of the error. So it would be better if you show the full definition of function

int PowerSlots::play();

because for example it is not clear at least where variable lasttimewon is from.
Hey
If PowerSlots is derived from SlotMachine would that not mean that
if( SlotMachine::m_creditpool > 0 )
could be
if( m_creditpool > 0 )
?
I made
1
2
        int m_creditpool;
        int m_rewardpool;


into
1
2
        static int m_creditpool;
	static int m_rewardpool;


it seems that now since its a static data member it can be accessed by other classes. It feels weird though because according to :
http://publib.boulder.ibm.com/infocenter/lnxpcomp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8l.doc%2Flanguage%2Fref%2Fcplr038.htm
static members can be accessed by other static members. but the only statics i have are the two data int's, seems to work though, but does anyone see anything off?

@vlad

you are correct, lasttimewon has not beed created, i just got stomped trying to access those data members. im not sure how but i need to figure out how to do a check for how many times the user has won... i might add it as a function on PowerSlots.



@ jidder
Hey
If PowerSlots is derived from SlotMachine would that not mean that
if( SlotMachine::m_creditpool > 0 )
could be
if( m_creditpool > 0 )
?


It doesnt seem to work that way for me. It's definately a derived class, but taking the SlotMachine:: off gives me an error.
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
#include <iostream>
using namespace std;
  class SlotMachine
{
public:
	int m_creditpool ;
	int m_rewardpool;
	SlotMachine():m_creditpool(1), m_rewardpool(1){}
	void play();
	void add_quarters( int quarters );
	int check_quarters() const;
};
class PowerSlots : public SlotMachine
{
public:
	void play();
	void PowerPlay();
	PowerSlots(){}
};

void PowerSlots::play()
{
	if( m_creditpool > 0 )
	{
		cout << "hello" ;
	}
}
int main()
{
PowerSlots Slot;
Slot.play();
}


i just made this quick so it just says hello if creditpool is more than 0.
What error did you get when you took off SlotMachine:: ?
That it is undefined..
but heres my PowerSlots constructor...
1
2
3
4
5
PowerSlots::PowerSlots()
{
	SlotMachine::m_creditpool = 0;
	SlotMachine::m_rewardpool = 0;
}


and if i take SlotMachine:: off from here i also get a undefined error
Powerslots doesn't really need a constructor unless it has its own variables.
As it is derived from SlotMachine both of these are handled by SlotMachines constructor.
In effect you are trying to construct them twice.
Please show sample code that can be pasted and compiled that to reproduce the error. It is not correct to speak about the error when we do not see the code.
Last edited on
powerslots.cpp is not obviously not finished...

slotmachine.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#ifndef __SLOTMACHINE_H__
#define __SLOTMACHINE_H__

#include<iostream>

/*
	Slot Machine class
	Models the behavior of a slot machine
*/
class SlotMachine
{
public:
	static int m_creditpool;
	static int m_rewardpool;
	SlotMachine();
	int play();
	void add_quarters( int quarters );
	int check_quarters() const;
};

#endif // __SLOTMACHINE_H__ 

slotmachine.cpp
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
35
36
37
38
39
40
#include<stdlib.h>
#include"slotmachine.h"

int SlotMachine::play()
{ 
	 //won = false;
	if( m_creditpool > 0 )
	{
		m_rewardpool += 1;
		m_creditpool -= 1;

		if(m_rewardpool >= 2)
		{
			bool won = rand()%2 == 0;
			if( won )
			{
				m_rewardpool -= 2;
				return 2;
			}
		}
	}
	
	return 0;
}

void  SlotMachine::add_quarters( int quarters )
{
	m_creditpool += quarters; 
}

int SlotMachine::check_quarters() const
{
	return m_creditpool;
}

SlotMachine::SlotMachine()
{
	m_creditpool = 0;
	m_rewardpool = 0;
}

powerslots.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#ifndef __POWERSLOTS_H__
#define __POWERSLOTS_H__

#include <iostream>

class PowerSlots : public SlotMachine
{
public:
	int play();
	int PowerPlay();
	PowerSlots();
	int lasttimewon();
};

#endif 


powerslots.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include"PowerSlots.h"
#include<stdlib.h>
#include"slotmachine.h"

PowerSlots::PowerSlots()
{
	SlotMachine::m_creditpool = 0;
	SlotMachine::m_rewardpool = 0;
	
}

int PowerSlots::play()
{
	if( SlotMachine::m_creditpool > 0 )
	{
		if( lasttimewon < 5 )
		{
			m_creditpool--;
			m_rewardpool++;
		}
	}
}

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
35
36
37
38
39
40
#include "slotmachine.h"
#include<time.h>
#include<iostream>

int main()
{
	srand(time(NULL)); //seed
	SlotMachine one;
	
	int quarters;
	std::cout << "Enter how many quarters you want to play: ";
	std::cin >> quarters;
	std::cout << "\n";
	one.add_quarters(quarters);

	while( one.check_quarters() > 0 )
	{
		for(int i = 0; i < quarters; ++i)
		{
			one.add_quarters(quarters);
		}
		quarters = 0;
		quarters += one.play();
		if(quarters == 0)
		{
			std::cout << "You lost." << std::endl;
		}
		else
		{
			std::cout << "you win. " << std::endl;
		}
	}

	std::cout << "You have: " << one.check_quarters() << "quarters";

	system("pause");

	return 0;
}
One again what is lasttimewon? Why is it compared with 5 in the statement

if( lasttimewon < 5 )

I see that there is declared such member function

int lasttimewon();

In the expression above you are comparing pointer to function with 5.

Also function play() does not return any value.

1
2
3
4
5
6
7
8
9
10
11
int PowerSlots::play()
{
	if( SlotMachine::m_creditpool > 0 )
	{
		if( lasttimewon < 5 )
		{
			m_creditpool--;
			m_rewardpool++;
		}
	}
}



I prepared a sample code and it is compiled successfully

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
35
36
37
#include <iostream>
 
class SlotMachine
{
public:
    int m_creditpool;
        int m_rewardpool;
        SlotMachine();
        int play();
        void add_quarters( int quarters );
        int check_quarters() const;
};
 
class PowerSlots : public SlotMachine
{
public:
    int play();
        int PowerPlay();
        PowerSlots();
        int lasttimewon();
};
 
int PowerSlots::play()
{
    if( SlotMachine::m_creditpool > 0 )
        {
        return 1;
        }
    
    return -1;
}
 
 
int main()
{
    
}


So there is no problem with statement

if( SlotMachine::m_creditpool > 0 )

this is what i have left to do for the power play class:

- Override the play function so that it lets a player win at most once every 5 tries.
- Add a function PowerPlay() that consumes 2 quarters but if the player wins it pays out half of its reward pool.
- Don’t let the player win the power play if there are less than 10 quarters in the machine.
Just because something compiles does not neccesarily mean there is no problem with it.
My code also compiled successfully without the SlotMachine part.
Also i dont understand why
SlotMachine::m_creditpool
and
SlotMachine::m_rewardpool
are constructed twice ?
Some of the code just seems a bit redundant.

Also i dont understand why
SlotMachine::m_creditpool
and
SlotMachine::m_rewardpool
are constructed twice ?
Some of the code just seems a bit redundant.

well thanks.
i constructed it twice trying to solve my original problem of accessing data members. which i solved.
Topic archived. No new replies allowed.