Car オブジェクトの色とノイズを印刷したいと思います。Car クラス内で c++11 の enum クラスを使用しようとしています。コンパイルすると、Car::Color and Car::Noise is not a class, namespace, or scoped enumeration というエラーが表示されます。:: 演算子を使用して enum クラスにアクセスしています。しかし、エラーは解決しません。問題は Car.cpp ファイルにあるようです。これが私のコードです。どんなアドバイスでも大歓迎です。本当にありがとう。
車.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;
};
車.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();
}