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.
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.
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.
#ifndef __SLOTMACHINE_H__
#define __SLOTMACHINE_H__
#include<iostream>
/*
Slot Machine class
Models the behavior of a slot machine
*/
class SlotMachine
{
public:
staticint m_creditpool;
staticint m_rewardpool;
SlotMachine();
int play();
void add_quarters( int quarters );
int check_quarters() const;
};
#endif // __SLOTMACHINE_H__
#ifndef __POWERSLOTS_H__
#define __POWERSLOTS_H__
#include <iostream>
class PowerSlots : public SlotMachine
{
public:
int play();
int PowerPlay();
PowerSlots();
int lasttimewon();
};
#endif
#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()
{
}
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.