读书人

重载++和-的一个有关问题

发布时间: 2013-11-25 13:22:27 作者: rapoo

重载++和--的一个问题

//Point 中x,y分别代表8进制的两位
//重载左右++,左右--
#include <iostream>
using namespace std;
class Point{
private:
int x,y;
public:
Point(int x=0,int y=0):x(x),y(y){}
Point& operator++(); //重载前置++(重载为成员函数)
Point operator++(int); //重载后置++(重载为成员函数)

Point& operator-- ();
Point operator-- (int);
void display()const;
};

Point& Point::operator++(){
y++;
if(y>=8){
y-=8;
x++;
cout<<"hello hello "<<x<<endl; //注意这里啊
if(x>=8){
x-=8;
}
}
return *this;
}

Point Point::operator ++(int){
Point old=*this;
++(* this);
return old;
}

Point& Point::operator--(){
y--;
if(y<0){
y=7;
x--;
cout<<"Hello "<<x<<endl; //注意这里啊
if(x<0){
x=7;
}
}
return *this;
}

Point Point::operator--(int){
Point old=*this;
//cout<<"zf"<<endl;
--(* this);
//cout<<"lxp"<<endl;
return old;
}

void Point::display()const{
cout<<"("<<x<<","<<y<<")"<<endl;
}

int main()
{
Point p1(7,7),p2(7,7);
cout<<"p1的值:";
p1.display();
cout<<"p2的值:";
p2.display();
cout<<"++p1的值是:";
(++p1).display();

cout<<"p2++的值是:";
(p2++).display(); //这里会调用 cout<<"hello hello "<<x<<endl;

Point p3(0,0),p4(7,7);
cout<<"p3的值:";
p3.display();
cout<<"p4的值:";
p4.display();

cout<<"--p3的值是:";
(--p3).display();

cout<<"p4--的值是:";
(p4--).display(); //为什么这里不调用 cout<<"Hello "<<x<<endl;
return 0;
}

问题写在程序里面了,求助啊
[解决办法]
调试啊调试 为什么不调试
[解决办法]
直接用vs 好了
[解决办法]
if(y<0){
y=7;
x--;
cout<<"Hello "<<x<<endl; //注意这里啊
[解决办法]
引用:
Quote: 引用:

if(y<0){
y=7;
x--;
cout<<"Hello "<<x<<endl; //注意这里啊
好吧,可以讲清楚点吗?真的不懂啊

你自己算算看,你的代码里会不会满足 y<0;
我只是大概看了一下,似乎不会。
[解决办法]
新手必须学会设置断点,逐步调试。分析代码。这样才能让你更好的解决问题

读书人网 >C++

热点推荐