挿入演算子と呼ばれていると思われる '<<' 演算子のオーバーロードに成功しました。カード オブジェクトのインスタンスの情報を出力する印刷関数があります。演算子を使用するときにこの印刷関数を呼び出すにはどうすればよいですか
例:
Card aCard("Spades",8); //generates an 8 of spades card object
aCard.Print(); // prints the suit and value of card
cout << aCard << endl; // will print something successfully but how can I get the same results as if I were to call the print function?
私の実装ファイルcard.cpp
では、カード クラスで使用するために << をオーバーロードしました。
Card.cpp
void Card::Print()
{
std::cout << "Suit: "<< Suit << std::endl;
std::cout << "Value:" << Value << std::endl;
}
std::ostream& operator<<(std::ostream &out, const Card &aCard)
{
Print();//this causes an error in the program
}
カード.h
class Card
{
public:
std::string Suit;
int Value;
Card(){};
Card(std::string S, int V){Suit=S; Value=V};
void Print();
friend std::ostream& operator<<(std::ostream&, const Card&)
};