2017-12-03 7 views
0

に演算子オーバーロード、私はここで私は、メソッドのプリントを持っていると演算子をオーバーロードカードのクラス、(カードの比較)メンバーがプライベートで、C++

を作成するには、私のコードです:

#include <iostream> 
#include <vector> 
#include <string> 
#include <stdexcept> 
using namespace std; 

enum Face {Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen,King, Ace}; 
enum Suit {Hearts,Clubs,Diamonds,Spades}; 

class Card{ 
private: 
    Face face; 
    Suit suit; 

public: 
    Card(); 
    Card(const Face face,const Suit suit); 
    void print(); 
    friend bool operator>(Card& lhs,Card&rhs); 
    friend bool operator==(Card& lhs,Card& rhs); 
    friend bool operator!=(Card& lhs,Card& rhs); 
    friend bool operator<(Card& lhs,Card& rhs); 

}; 

const char* to_string_1(const Face value) 
{ 
    const char* LUT[] = {"Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King", "Ace" }; 
    return LUT[value]; 
} 

const char* to_string_2(const Suit value) 
{ 
    const char* LUT[] = {"Hearts", "Clubs", "Diamonds", "Spades" }; 
    return LUT[value]; 
} 



Card::Card(){ 
} 

Card::Card(Face rank_card,Suit suit_card){ 
    face = rank_card; 
    suit = suit_card; 
} 




void Card::print(){ 
    cout << to_string_1(face) << " " << to_string_2(suit); 
} 



bool operator==(const Card& lhs, const Card& rhs){ 
    if(lhs.face == rhs.face && lhs.suit == rhs.suit) return true; 
    return false; 
} 

bool operator!=(const Card& lhs,const Card& rhs){ 
    if(lhs.face != rhs.face || lhs.suit != rhs.suit) return true; 
    return false; 
} 

bool operator>(const Card& lhs,const Card& rhs){ 
    if(lhs.face > rhs.face && lhs.suit > rhs.suit) return true; 
    if(lhs.face > rhs.face && lhs.suit == rhs.suit) return true; 
    if(lhs.face == rhs.face && lhs.suit > rhs.suit) return true; 
    return false; 
} 

bool operator<(const Card& lhs,const Card& rhs){ 
    if(lhs.face < rhs.face && lhs.suit < rhs.suit) return true; 
    if(lhs.face < rhs.face && lhs.suit == rhs.suit) return true; 
    if(lhs.face == rhs.face && lhs.suit < rhs.suit) return true; 
    return false; 
} 

int main(){ 
    Card c_1(Two,Clubs); 
    Card c_2(Five,Diamonds); 
    c_2.print(); 
} 

問題そのコンパイラは次のようなエラーを投げます:

12:10:エラー: 'Face Card :: face'はプライベート 58:24:エラー:このコンテキスト内 13:10:エラー: 'Suit Card ::スーツ 'はプライベートです 5 8:36:エラー:このコンテキスト内

しかし、私は演算子を「フレンド」関数として定義しました。なぜプログラマは文句を言うのですか?

+3

フレンド宣言には「const」がありません。 – VTT

答えて

1

友人の署名と宣言は正確に一致しなければなりません。あなたはあなたの友人の宣言であなたのconstを忘れてしまった

bool operator>(const Card& lhs,const Card& rhs){ 

friend bool operator>(Card& lhs,Card&rhs); 

の比較。

関連する問題