class pointer {
int x, y;
public:
void swap(int *px, int *py)
{ int temp=*px;
*px=*py;
*py=temp;
}
friend void disp(int &fx, int &fy);
};//pointer
class reference {
int x, y;
public:
void swap(int &x, int &y)
{ int temp=x;
x=y;
y=temp;
}
friend void disp(int &fx, int &fy);
};//reference
void disp(int &fx, int &fy)
{ cout<< fx <<","<< fy; }
void main()
{
pointer a;
reference b;
int x=11, y=19, xx=1, yy=9;
a.swap(&xx,&yy);
b.swap(&x,&y);
disp(a.x, a.y);
disp(b.x, b.y);
}//mian
天哪!怎麼物件導向的程式這麼難啊!
連這麼簡單的程式我都寫不出來!
可不可以教教我啊!我實在是看不出來到底哪裏錯了!
才幾行程式碼花了我二個小時,還看不出來到底哪裏錯了。
Compiling ..\..\13CPP\CPP\0536.CPP:
Warning ..\..\13CPP\CPP\0536.CPP 38: Temporary used for parameter 'x' in
call to 'reference::swap(int &,int &)'
Error ..\..\13CPP\CPP\0536.CPP 38: Cannot convert 'int *' to 'int'
Error ..\..\13CPP\CPP\0536.CPP 38: Type mismatch in parameter 'x' in call to
'reference::swap(int &,int &)'
Warning ..\..\13CPP\CPP\0536.CPP 38: Temporary used for parameter 'y' in
call to 'reference::swap(int &,int &)'
Error ..\..\13CPP\CPP\0536.CPP 38: Cannot convert 'int *' to 'int'
Error ..\..\13CPP\CPP\0536.CPP 38: Type mismatch in parameter 'y' in call to
'reference::swap(int &,int &)'
Error ..\..\13CPP\CPP\0536.CPP 39: 'pointer::x' is not accessible
Error ..\..\13CPP\CPP\0536.CPP 39: 'pointer::y' is not accessible
Error ..\..\13CPP\CPP\0536.CPP 40: 'reference::x' is not accessible
Error ..\..\13CPP\CPP\0536.CPP 40: 'reference::y' is not accessible
Warning ..\..\13CPP\CPP\0536.CPP 41: 'y' is assigned a value that is never
used
Warning ..\..\13CPP\CPP\0536.CPP 41: 'x' is assigned a value that is never
used
請看我前面幫你標示起來的地方:
你怎麼可以將一個 指標( x ) 傳給 一個整數變數( temp )呢
(相反也不行............)
還有 你的reference class 有 x,y 兩個變數成員,
你的swap函式怎麼又宣告成:
swap(int &x,int &y)
x,y 有重複了嘛.............
--
>>>>my name is oel larrel<<<<<
想清算我,討伐我,鬥垮我的人,請email oel
謝謝
--
※ Origin: 程式設計樂園 ◆ From: tp220-4.dialup.seed.net.tw
class pointer {
int x, y;
public:
pointer(int p_x, int p_y)
{ x=p_x; y=p_y; }
void swap(int *px, int *py)
{ int temp=*px;
*px=*py;
*py=temp;
}
friend void disp(int fx, int fy);
};//pointer
class reference {
int x, y;
public:
reference(int r_x, int r_y)
{ x=r_x; y=r_y; }
void swap(int &rx, int &ry)
{ int temp=rx;
rx=ry; ry=temp;
}
friend void disp(int fx, int fy);
};//reference
void disp(int fx, int fy)
{ cout<<fx<<","<<fy; }
void main()
{
int x=11, y=19, xx=1, yy=9;
pointer a(xx, yy);
reference b(x, y);
a.swap(&xx,&yy);
b.swap(x,y);
disp(a.x, a.y);//這裏錯了
disp(b.x, b.y);//還有這裏
}//mian
謝謝你,經過我一番修改了之後,變成這樣,可是還是有錯.
不是說,朋友函數,可以取用類別的成員嗎?為什麼這裏不行?