Class named light

Hello I was attempting to construct a a class named Light that simulates a traffic light. The class’s color
attribute should change from Green to Yellow to Red and then back to Green by using the
class’s change() method. When a new Light object is created, its initial color should be Red. I kept getting an error on my program every time I tried to run it and I was wondering if someone could help me. Thank you
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
  #include <iostream>
#include <string>
#include <iomanip>
using namespace std;
class Light
{
private:
bool mRed, mYellow, mGreen;
string color;    
                   
public:
Light();
void chgColor();
};
Light::Light() :
mRed(true), mYellow(false), mGreen (false) {
}
void Light::chgColor(){
if (mGreen) {
mGreen = false;
mYellow =true;
}
else {
mRed = false;
mGreen = true;
}
};
string Light::getColor() {
if (mGreen)
return "Green";
else if (mYellow)
return "Yellow";
else return "Red";
};
int main()
{
Light light;
light.chgColor();
system("pause");
return 0;
}
Your class doesn't have a std::string getColor() method.
Last edited on
I kept getting an error

What does that error say?

(It is a compiler error, not a linker error nor a runtime error.)
the error is

28 C:\Users\awesome\Code\10.4.cpp no `std::string Light::getColor()' member function declared in class `Light'

Also to xismn could you explain how I could input that method when it comes to strings I'm a little lost.
Your class definition ends after line 14. He simply means that you don't have the declaration of a getColor() member function in your class definition. You need a declaration, in this case
std::string getColor(); within your class definition.
Listen to the error, in this case it was pretty much right there: "no [...]getColor() member function declared in class "Light""
Last edited on
Ok I added that, but I need help with my code. I'm trying to have the person input a color and depending on what color they input determines what the next two colors will be. For example if I input red the next color will be green then Yellow. Here is what I have so far.

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
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
class Light
{
private:
bool mRed, mYellow, mGreen;   
                   
public:
Light();
void chgColor();
std::string getColor();
};
Light::Light() :
mRed(true), mYellow(false), mGreen (false) {
}
void Light::chgColor(){
if (mGreen) {
mGreen = false;
mYellow =true;
}
else {
mRed = false;
mGreen = true;
}

};
string Light::getColor() {
if (mGreen)
return "Green";
else if (mYellow)
return "Yellow";
else return "Red";
};
int main()
{
    char color;
    cout << "Input a color Green, Red, or Yellow: ";
            cin >> color;
Light light;
light.chgColor();
system("pause");
return 0;
}
Complex if..else.. are complex. It is simpler to calculate.

There is a concept of iterator. Incrementing iterator makes the iterator point to the "next". Does that sound promising? Changing to the next could be a simple ++current;

However, you have a cyclic list. The next of that last item on the list is the first item on the list. Have you used the modulus operator? current = (current + 1) % count;

That lets us to have a "current" that has a value within [0..count]. (Thus, if count==3, current can be 0, 1, or 2.) One numeric variable to remember which colour you have.


What is left is to print name of colour based on the value of current. You can refer to element in array with index.


Ok, one more thing. Your program does not set the current based on the user input. Your class requires a way to do that. You could have a constructor, or a "setter" method. Essentially, you get a name of colour and figure out which index matches that.
Ok so I ended up solving the problem. Thank you keskiverto for your help same with xismn and Ganado.
Topic archived. No new replies allowed.