C++ 在运算符重载函数中使用运算符重载
我有以下类(用C++):C++ 在运算符重载函数中使用运算符重载,c++,operator-overloading,C++,Operator Overloading,我有以下类(用C++): 类卡 { 公众: //其他职能 std::ostream&operator您不能将插入作为成员函数重载到ostream,因为第一个参数是ostream。您需要将其设置为自由函数: std::ostream& operator<<(std::ostream&, Card const &); std::ostream&operator 使用此代码…将其定义为一个自由函数,它将std::ostream&作为第一个参数,您的类作为第二个参数
类卡
{
公众:
//其他职能
std::ostream&operator您不能将插入作为成员函数重载到ostream
,因为第一个参数是ostream
。您需要将其设置为自由函数:
std::ostream& operator<<(std::ostream&, Card const &);
std::ostream&operator
使用此代码…将其定义为一个自由函数,它将std::ostream&
作为第一个参数,您的类作为第二个参数。
class Hand
{
public:
std::ostream& operator<< (std::ostream& os)
{
for (std::vector<Card>::iterator iter = cards.begin(); iter < cards.end(); ++iter)
os << *iter << ", "; //THIS LINE PRODUCES THE ERROR
return os;
}
private:
std::vector<Card> cards;
};
std::ostream& operator<<(std::ostream&, Card const &);
Card c;
c << std::cout;
public:
Suit suit;
Rank rank; //these are both pre-defined enums