2016-11-15 2 views
0

ここで私は列挙型クラスがあります。「通常の」演算子を使用する代わりに、オーバーロードされた演算子を使用するようにコンパイラに指示するにはどうすればよいですか?

enum class wahl { 
    schere , stein , papier 
}; 

をそして私は、私は別のクラス、名前のプレーヤー持っているオペレータ<と>

bool operator<(wahl &wahl1, wahl &wahl2) { 
    switch (wahl1) { 
    case wahl::papier: { 
     if (wahl2 == wahl::schere) { 
      return true; break; 
     } //papier < schere 
     else if (wahl2 == wahl::stein) { 
      return false; break; 
     } //papier > stein 
     else { 
      return false; break; 
     } 
    } 
    case wahl::schere: { 
     if (wahl2 == wahl::stein) { 
      return true; break; 
     } //schere < stein 
     else if (wahl2 == wahl::papier) { 
      return false; break; 
     } //schere > papier 
     else { 
      return false; break; 
     } 
    } 
    case wahl::stein: { 
     if (wahl2 == wahl::papier) { 
      return true; break; 
     } //stein < papier 
     else if (wahl2 == wahl::schere) { 
      return false; break; 
     } //stein > schere 
     else { 
      return false; break; 
     } 
    } 
    } 
}; 

bool operator > (const wahl wahl1, const wahl wahl2) { 
switch (wahl1) { 
case wahl::papier: { 
    if (wahl2 == wahl::schere) { 
     return false; break; 
    } 
    // papier < schere 
    else if (wahl2 == wahl::stein) { 
     return true; break; 
    } //papier > stein 
    else { 
     return false; break; 
    } 
} 
case wahl::schere: { 
    if (wahl2 == wahl::stein) { 
     return false; break; 
    } //schere < stein 
    else if (wahl2 == wahl::papier) { 
     return true; break; 
    } //schere > papier 
    else { 
     return false; break; 
    } 
} 
case wahl::stein: { 
    if (wahl2 == wahl::papier) { 
     return false; break; 
    } //stein < papier 
    else if (wahl2 == wahl::schere) { 
     return true; break; 
    } //stein > schere 
    else { 
     return false; break; 

    } 
} 
} 
}; 

をオーバーロード:

class player { 
wahl pl_wahl; 
int pl_score; 
char* pl_name; 

public: 

player() {} 
player(int score, wahl wahl, char* name) : 
    pl_wahl{ wahl }, pl_score{ score }, pl_name{ name } {} 

wahl pl_get_wahl() { 
    return pl_wahl; 
} 

char* pl_get_name() { 
    return pl_name; 
} 

int &pl_get_score() { 
    return pl_score; 
} 
}; 

そして、ここに私はコンパレータを使用しました:

class game { 

player game_player1, game_player2, game_momentan_gewinner; 
int game_score_max; 

public: 
game() {} 
game(player player1, player player2, int score_max) : 
    game_player1{player1}, 
    game_player2{player2}, 
    game_score_max{ score_max } {} 

void vergleichen() { 
    if (game_player1.pl_get_wahl() > game_player2.pl_get_wahl()) { 
     game_momentan_gewinner = game_player1; 
     std::cout << "Gewinner dieser Runde ist Player 1 : " << 
      game_momentan_gewinner.pl_get_name() << std::endl; 
    } 
    if (game_player1.pl_get_wahl() < game_player2.pl_get_wahl()) { 
     game_momentan_gewinner = game_player2; 
     std::cout << "Gewinner dieser Runde ist Player 2 : " << 
      game_momentan_gewinner.pl_get_name() << std::endl; 
    } 
    else { 
     std::cout << "Remis" << std::endl; 
    } 

} 
}; 

私が持っている問題は、enumはintとして処理されます。そして、2つの列挙型変数を比較すると、その結果は、オーバーロードされた演算子で設定した値を取るのではなく、int値に依存します。

コンパイラがenum変数のint値を使用するのをやめて、オーバーロードされた演算子のような方法で列挙型変数を比較する方法はありますか?

+1

あなたはおそらく 'ブール演算子<(constのWAHL&wahl1、constのWAHL&wahl2)'や 'ブール演算子<(WAHLたいですwahl1、wahlwahl2) 'を実行します。 – Jarod42

+0

左辺値を比較しようとしていますか?多分、あなたのオペレータは参考にしてはいけません。 – aschepler

+0

ところで、演算子は厳密な順序付けを尊重しないので、いくつかの場所で(std :: sort(wahls.begin()、wahls.end())や 'std ::マップ ')。 – Jarod42

答えて

0

あなたは正しい署名を使用する場合は、[OK]をする必要があります:

bool operator<(wahl lhs, wahl rhs); 
bool operator>(wahl lhs, wahl rhs); 

Demo

関連する問題