class Hallway
{
private:
//---------------------------------------------------------------
// DO_04: Declare a Light array of size MAX_LIGHTS
// Hint: look through the methods below to find the name to use
// for the array
//---------------------------------------------------------------
int numLights;
int lights[MAX_LIGHTS];public:
//------------------------------------------------------------------
// Default constructor.
// Params: (none)
//------------------------------------------------------------------
Hallway()
{
numLights = 0;
}
//------------------------------------------------------------------
// This method models the actions of a person starting at the
// k-th light in the hallway and running to the end of the hallway
// toggling every k-th light along the way. It is very important
// that the loop-control variable i be initialized to k - 1!
// Valid range for k: 1 <= k <= numLights.
// Params: (in)
//------------------------------------------------------------------
void ToggleEveryKthLight(int k)
{
for (int i = k - 1; i < numLights; i += k)
lights[i].Toggle();
}
//------------------------------------------------------------------
// Reads in a hallway from the standard input. All of the lights
// in the hallway are initially in the off state.
// Params: (none)
//------------------------------------------------------------------
void Read()
{
cin >> numLights;
//---------------------------------------------------------------
// DO_05: Turn off all of the lights in the hallway. Hint: use
// a for loop and call the TurnOff() method for each light in
// the hallway.
//---------------------------------------------------------------
for (int i = 0; i < numLights; i++)
lights[i].TurnOff();
}
I keep getting the error " this.lights[i] is not a struct or class and so you cannot use '.' " on line 34.
How am I supposed to define my lights[] array? I can't modify anything except directly after each comment block.
Watch this line closely: lights[i].Toggle();
That line calls member function Toggle() on the element on position i in the lights array. But that array is of type int, and int doesn't have a member function Toggle().
Considering the rest of your code I think you should change this line:
int lights[MAX_LIGHTS];
to something like
Light lights[ MAX_LIGHTS ];
or whatever the name of your light class is. In any case that class should provide the member functions Toggle() and TurnOff()
void Write() const
{
//---------------------------------------------------------------
// DO_06: Write out the hallway to the standard output. Hint:
// use a for loop to call the Write() method of each light in
// the hallway. Be sure to conform exactly to the output
// conventions, i.e., the status of each light--one per line.
// Note that the first light is to be displayed as "Light 1" and
// NOT "Light 0".
//---------------------------------------------------------------
for (int i = 0; i < numLights; i++)
cout << "Light " << i +1 << " is " << Light.Write() << "." << endl;
}
Now I'm having trouble calling the Write function from the Light class. There are two Write functions, this one above, and another one in a different class called Light. How do I call the Write function from class Light?