0

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();
}
3
  • 2
    Works for me Commented May 14, 2016 at 3:46
  • 1
    What compiler do you use? Commented May 14, 2016 at 3:48
  • yep I think I have an outdated compiler. that'll do it Commented May 14, 2016 at 4:02

1 Answer 1

0

My compiler was outdated. All's working now.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.