I want to print my Car object's color and noise. I'm trying to use c++11's enum class within my Car class. When I compile I get error Car::Color and Car::Noise is not a class, namespace, or scoped enumeration. I am using the :: operator to access the enum class. But the error persists. The problem seems to lie in the Car.cpp file. Here is my code. Any advice is much appreciated. Thanks so much.
Car.h
class Car {
public:
enum class Color {red, yellow, blue};
enum class Noise {loud, soft, medium};
void setColor();
void setNoise();
void getColor();
void getNoise ();
private:
Color c;
Noise n;
};
Car.cpp
#include<iostream>
#include "Car.h"
using namespace std;
void Car::setColor() {
c = Color::red;
}
void Car::setNoise() {
n = Noise::loud;
}
void Car::getColor() {
switch(c) {
case Color::red: cout << "red" << endl;
case Color::yellow: cout << "yellow" << endl;
case Color::blue: cout << "blue" << endl;
default:
cout << "error" << endl;
}
}
void Car::getNoise() {
switch(n) {
case Noise::loud: cout << "loud" << endl;
case Noise::soft: cout << "soft" << endl;
case Noise::medium: cout << "medium" << endl;
default: cout << "error" << endl;
}
}
main.cpp
#include <iostream>
#include "Car.h"
int main() {
Car car;
car.setColor();
car.setNoise();
car.getColor();
car.getNoise();
}