在重载的 I/O 运算符中重载增量运算符时出错
Error while overloading increment operator inside overloaded I/O operator
我是 OOPS 概念的初学者。我现在正在研究运算符重载。当我在 cout
中使用重载的增量运算符时,我 运行 进入错误 no match for operator<<
。当我从 cout 中删除重载的增量时,它工作正常。从逻辑上讲,我不觉得代码有什么问题。不过,我不知道为什么会出现错误?下面是我的代码,可以更好地理解这个问题。
#include <iostream>
using namespace std;
class Digit
{
private:
int digit;
public:
Digit(int n)
{
digit = n;
}
Digit& operator++();
Digit operator++(int); //overloaded postfix increment. Dummy argument used
Digit& operator--();
Digit operator--(int); //overloaded postfix decrement. Dummy argument used
friend ostream& operator<<(ostream& out, Digit& x); //overloaded << prototype
int GetDigit()
{
return digit;
}
};
Digit Digit::operator++(int)
{
//Create a temporary object with a variable
Digit temp(digit);
//Use prefix operator to increment this Digit
++(*this);
return temp; //return temporary result
}
Digit& Digit::operator++()
{
if (digit==9)
digit = 0;
else
++digit;
return *this;
}
Digit Digit::operator--(int)
{
//Create a temporary object with a variable
Digit temp(digit);
//Use prefix operator to increment this Digit
--(*this);
return temp; //return temporary result
}
Digit& Digit::operator--()
{
if (digit==0)
digit = 9;
else
--digit;
return *this;
}
int main()
{
using namespace std;
Digit n(9);
Digit x(0);
cout << n++ << endl;
cout << x-- << endl;
return 0;
}
ostream& operator<<(ostream& out, Digit& x)
{
out << x.digit;
return out;
}
main()
中的 cout << n++ << endl; cout << x-- << endl;
行导致错误。
这是因为后缀运算符 return 按值 ,如果你不保存该值,你将有一个临时的,非常量引用不能'绑定到临时文件。
简单的解决方法是让您的输出运算符通过 const
引用获取 Digit
参数:
ostream& operator<<(ostream& out, Digit const& x)
// ^^^^^
我是 OOPS 概念的初学者。我现在正在研究运算符重载。当我在 cout
中使用重载的增量运算符时,我 运行 进入错误 no match for operator<<
。当我从 cout 中删除重载的增量时,它工作正常。从逻辑上讲,我不觉得代码有什么问题。不过,我不知道为什么会出现错误?下面是我的代码,可以更好地理解这个问题。
#include <iostream>
using namespace std;
class Digit
{
private:
int digit;
public:
Digit(int n)
{
digit = n;
}
Digit& operator++();
Digit operator++(int); //overloaded postfix increment. Dummy argument used
Digit& operator--();
Digit operator--(int); //overloaded postfix decrement. Dummy argument used
friend ostream& operator<<(ostream& out, Digit& x); //overloaded << prototype
int GetDigit()
{
return digit;
}
};
Digit Digit::operator++(int)
{
//Create a temporary object with a variable
Digit temp(digit);
//Use prefix operator to increment this Digit
++(*this);
return temp; //return temporary result
}
Digit& Digit::operator++()
{
if (digit==9)
digit = 0;
else
++digit;
return *this;
}
Digit Digit::operator--(int)
{
//Create a temporary object with a variable
Digit temp(digit);
//Use prefix operator to increment this Digit
--(*this);
return temp; //return temporary result
}
Digit& Digit::operator--()
{
if (digit==0)
digit = 9;
else
--digit;
return *this;
}
int main()
{
using namespace std;
Digit n(9);
Digit x(0);
cout << n++ << endl;
cout << x-- << endl;
return 0;
}
ostream& operator<<(ostream& out, Digit& x)
{
out << x.digit;
return out;
}
main()
中的 cout << n++ << endl; cout << x-- << endl;
行导致错误。
这是因为后缀运算符 return 按值 ,如果你不保存该值,你将有一个临时的,非常量引用不能'绑定到临时文件。
简单的解决方法是让您的输出运算符通过 const
引用获取 Digit
参数:
ostream& operator<<(ostream& out, Digit const& x)
// ^^^^^