Declaring an Array inside a class. 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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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.
Last edited on
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()
Oh ok, gotcha. There are two classes, Hallway and Light. Light contains the toggle function. It makes sense now, thanks!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
 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?
The same way as you'd call any other method on an object:

myObject.myMethod(int myParam);
myObject = Light

myMethod = Write( ) (its a void function with no params)

so why isn't Light.Write( ) working? The error is that it's expecting a ( before the dot.
myObject = Light

No. Light is the class, not the object.

lights[i] is your object.
Topic archived. No new replies allowed.