Re: [問題] 關於運算子多載的問題

看板C_Sharp (C#)作者 (垃圾桶)時間20年前 (2005/09/28 11:32), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串2/2 (看更多)
※ 引述《GasolLee (嗨...)》之銘言: : 各位大大好 : 我寫了一個小程式來練習C#運算子多載的功能 : 程式如下 : using System; : public struct Point : { : public int x, y; : public Point(int x, int y) : { : this.x = x; : this.y = y; : } : //以下省略節省版面 : } : class A : { : public static void Main() : { : Point p1 = new Point(0, 0); : Point p2 = new Point(100, 10); : Point p3; : p3 = p1 + p2; : //p3 = p1+p2; : Console.WriteLine("p3.X = {0}, p3.Y = {1}", p3.X, p3.Y); : } : public static Point operator+(Point P1, Point P2)//, Point P2) : { : Point P; : P.x = 1; : P.y = 2; : return P1; : } : } : 我在編譯這個小程式的時候 : 一直出現以下的錯誤 : One of the parameters of a binary operator must be the containing type : 請問這是什麼意思呢??? operator這個method 參數所用的類別必須跟你定義的類別相符 你應該寫在public struct Point裡面 來定義Point的operator 而不是在class A裡面定義其他類別的operator -- 賭 輸 錢 了 , 再 多 打 幾 個 字 . O r z -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 59.104.83.32
文章代碼(AID): #13EWwqmM (C_Sharp)
文章代碼(AID): #13EWwqmM (C_Sharp)