我使用::运算符,但我仍然得到枚举类错误不是类,命名空间或作用域枚举的错误

时间:2016-05-14 03:43:22

标签: c++11 enum-class

我想打印Car对象的颜色和噪点。我试图在我的Car类中使用c ++ 11的枚举类。当我编译时,我得到错误Car :: Color和Car :: Noise不是类,命名空间或作用域枚举。我使用::运算符来访问枚举类。但错误仍然存​​在。问题似乎在于Car.cpp文件。这是我的代码。任何建议都非常感谢。非常感谢。

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();
}

1 个答案:

答案 0 :(得分:0)

我的编译器已经过时了。所有人现在都在工作。