[英]postfix prefix operator overload error c++
将运算符重载用于前缀和后缀增量时,我从编译器收到错误:
“ Fajl Fajl :: operator ++(int)':成员函数已定义或声明”
这是我的运算符++标头:
Fajl& operator ++ (); // prefix
Fajl& operator -- (); // prefix
Fajl operator ++ (int); // postfix
Fajl operator ++ (int); // postfix
和我的实现:
Fajl& Fajl::operator ++ () // prefix
{
++(*poz);
return *this;
}
Fajl& Fajl::operator -- () // prefix
{
--(*poz);
return *this;
}
Fajl Fajl::operator ++ (int dummy) // postfix
{
Fajl temp(*this);
++(*this);
return temp;
}
Fajl Fajl::operator -- (int dummy) // postfix
{
Fajl temp(*this);
--(*this);
return temp;
}
“ Fajl”是类,“ poz”是我要增加的自变量。 我究竟做错了什么?
Fajl operator ++ (int); // postfix
Fajl operator ++ (int); // postfix
^^
should be --
很难确定,但这也许是指您已经重复了后缀增量的声明:
Fajl operator ++ (int); // postfix
Fajl operator ++ (int); // postfix
大概其中之一应该是--
而不是++
。 这很可能会导致另一个问题:您显然已经定义了一个operator--
,但是未在类定义中声明。 如果还没有,编译器几乎肯定也会对此抱怨。
您有2个相同的Fajl运算符++(int)声明; 将第二个更正给操作员-
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.