0

这是我的后增量运算符重载声明。

loc loc::operator++(int x)
{
    loc tmp=*this;
    longitude++;
    latitude++;
    retrun tmp;
} 

我的类构造函数

loc(int lg, int lt) 
{
   longitude = lg;
   latitude = lt;
}

在主要功能中,我编写了如下代码

int main()
{
    loc ob1(10,5);
    ob1++;
}

编译时,我收到以下错误

opover.cpp:56:5:错误:'loc loc::operator++(int)' 的原型与类'loc' opover.cpp:49:5 中的任何内容都不匹配:错误:候选者是:loc loc::operator++( ) opover.cpp:在函数'int main()'中:opover.cpp:69:4:错误:没有为后缀'++'声明'operator++(int)'</p>

4

3 回答 3

4

修复你的类声明

class loc
{
    // ...
    loc operator++();
} 

class loc
{
    // ...
    loc operator++(int);
} 

[编辑删除了关于按值返回的误导性言论。按值返回当然是后缀运算符++的常用语义]

于 2011-11-04T07:56:19.803 回答
3

你应该有两个版本++

loc& loc::operator++() //prefix increment (++x)
{
    longitude++;
    latitude++;
    return *this;
} 

loc loc::operator++(int) //postfix increment (x++)
{
    loc tmp(longtitude, langtitude);
    operator++();
    return tmp;
}

当然,这两个函数都应该在类原型中定义:

loc& operator++();
loc operator++(int);
于 2011-11-04T07:58:38.903 回答
1

您没有在类定义中声明重载运算符。

你的班级应该是这样的:

class loc{
public:
    loc(int, int);
    loc operator++(int);
    // whatever else
}

** 编辑 **

阅读评论后,我注意到在您的错误消息中它显示您声明了loc operator++(),所以只需修复它。

于 2011-11-04T07:55:42.353 回答