Re: [問題] 關於運算子多載的問題
※ 引述《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
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 2 之 2 篇):
C_Sharp 近期熱門文章
PTT數位生活區 即時熱門文章