enum
タイプをインクリメントするオーバーロード演算子を書きたい:
#include <iostream>
#include <vector>
#include <string>
#include <stdexcept>
using namespace std;
enum Type {grun,grun_blinkend,gelb,rot ,rot_gelb,gelb_blinkend}
Type& operator++(Type& color){
return color = static_cast<Type>(++static_cast<int>(Type));
};
しかし、それは私にエラーを投げます:
エラー:「&」トークンの前に初期化子が必要です
なぜ、どうすれば修正できますか?
「信号機」シミュレーションのタイプを反復処理するには、この演算子が必要です。
Ampel Ampel::weiter(){
if(zustand == Type(rot_gelb)){
zustand = Type(grun);
return Ampel(zustand);
}
++zustand;
return Ampel(zustand);
}
これは解決策ですが、gelb_blinkendよりも高い整数に注意する必要があります。この質問を参照することをお勧めします。また、の変更についても検討する必要があります。このenum
ためenum class
、この別の質問をお勧めします。
enum Type {grun,grun_blinkend,gelb,rot ,rot_gelb,gelb_blinkend};
Type& operator++(Type& color){
int current = static_cast<int>(color);
return color = static_cast<Type>(++current);
};
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加