++ 前缀运算符重载不适用于 <<
++ pre-fix operator overload is not working with <<
这里有一些新的 C++ 开发人员。我有一个 class Rational
用于表示有理数并允许用户对它们执行算术和关系运算。我重载了所有运算符并且它们都正常工作,除非我尝试将 ++/--
前缀运算符(例如 --Rational
)或 +/-
一元运算符与 <<
输出运算符,如:
Rational num(7, 2); // initializes rational to 7/2 (fraction)
cout << --num; // should change val to 5/2 (decrements by one)
// error: no match for 'operator<<'(operand types are 'std::ostream' {aka 'std::basic_ostream<char>'} and 'Rational')
或:
Rational num(1, 10);
cout << -num; // should change to -1/10
// error: no match for 'operator<<'(operand types are 'std::ostream' {aka 'std::basic_ostream<char>'} and 'Rational')
我不明白为什么,因为我已经将所有这些运算符重载到 return 一个 Rational
对象,并且我已经重载了 <<
运算符以接受还有一个 Rational
对象。有趣的是,当我尝试这个时:
Rational num(5, 3);
cout << num;
它按预期工作。那么有人能告诉我这是怎么回事吗?
class的相关代码:
class Rational
{
private:
// Instance variables declarations
int numerator, denominator;
public:
// Constructors declarations
Rational(int numer_val, int denom_val = 1);
Rational();
// Unary operators declarations
friend Rational operator +(const Rational &num);
friend Rational operator -(const Rational &num);
friend Rational operator ++(Rational &num);
friend Rational operator --(Rational &num);
// I/O operators
friend ostream& operator <<(ostream &outs, Rational &num);
};
// Constructors definitions
Rational::Rational(int numer_val, int denom_val) : numerator(numer_val)
{
set_denominator(denom_val); // irrelevant
simplify(); // irrelevant
}
Rational::Rational() : Rational(0) { }
// Unary operators definitions
Rational operator +(const Rational &num)
{
return Rational(+num.numerator, num.denominator);
}
Rational operator -(const Rational &num)
{
return Rational(-num.numerator, num.denominator);
}
Rational operator ++(Rational &num)
{
num.numerator += num.denominator;
return num;
}
Rational operator --(Rational &num)
{
num.numerator -= num.denominator;
return num;
}
// I/O operators
ostream& operator <<(ostream &outs, Rational &num)
{
outs << to_string(num.numerator) + "/" + to_string(num.denominator);
return outs;
}
你的 operator<<
的第二个参数绑定到一个 l-value 引用:
ostream& operator <<(ostream &outs, Rational & num)
// ^
但是你所有的运营商return r-values。例如,当您执行以下操作时:
cout << -num;
您正在尝试将 l-value 引用绑定到 r-value,这是不允许的。
您可以通过更改 operator<<
以接受常量引用来解决此问题:
ostream& operator <<(ostream &outs, Rational const & num)
// ^^^^^^^
这里有一些新的 C++ 开发人员。我有一个 class Rational
用于表示有理数并允许用户对它们执行算术和关系运算。我重载了所有运算符并且它们都正常工作,除非我尝试将 ++/--
前缀运算符(例如 --Rational
)或 +/-
一元运算符与 <<
输出运算符,如:
Rational num(7, 2); // initializes rational to 7/2 (fraction)
cout << --num; // should change val to 5/2 (decrements by one)
// error: no match for 'operator<<'(operand types are 'std::ostream' {aka 'std::basic_ostream<char>'} and 'Rational')
或:
Rational num(1, 10);
cout << -num; // should change to -1/10
// error: no match for 'operator<<'(operand types are 'std::ostream' {aka 'std::basic_ostream<char>'} and 'Rational')
我不明白为什么,因为我已经将所有这些运算符重载到 return 一个 Rational
对象,并且我已经重载了 <<
运算符以接受还有一个 Rational
对象。有趣的是,当我尝试这个时:
Rational num(5, 3);
cout << num;
它按预期工作。那么有人能告诉我这是怎么回事吗?
class的相关代码:
class Rational
{
private:
// Instance variables declarations
int numerator, denominator;
public:
// Constructors declarations
Rational(int numer_val, int denom_val = 1);
Rational();
// Unary operators declarations
friend Rational operator +(const Rational &num);
friend Rational operator -(const Rational &num);
friend Rational operator ++(Rational &num);
friend Rational operator --(Rational &num);
// I/O operators
friend ostream& operator <<(ostream &outs, Rational &num);
};
// Constructors definitions
Rational::Rational(int numer_val, int denom_val) : numerator(numer_val)
{
set_denominator(denom_val); // irrelevant
simplify(); // irrelevant
}
Rational::Rational() : Rational(0) { }
// Unary operators definitions
Rational operator +(const Rational &num)
{
return Rational(+num.numerator, num.denominator);
}
Rational operator -(const Rational &num)
{
return Rational(-num.numerator, num.denominator);
}
Rational operator ++(Rational &num)
{
num.numerator += num.denominator;
return num;
}
Rational operator --(Rational &num)
{
num.numerator -= num.denominator;
return num;
}
// I/O operators
ostream& operator <<(ostream &outs, Rational &num)
{
outs << to_string(num.numerator) + "/" + to_string(num.denominator);
return outs;
}
你的 operator<<
的第二个参数绑定到一个 l-value 引用:
ostream& operator <<(ostream &outs, Rational & num)
// ^
但是你所有的运营商return r-values。例如,当您执行以下操作时:
cout << -num;
您正在尝试将 l-value 引用绑定到 r-value,这是不允许的。
您可以通过更改 operator<<
以接受常量引用来解决此问题:
ostream& operator <<(ostream &outs, Rational const & num)
// ^^^^^^^