作者glenn7012 (垃圾桶)
看板C_Sharp
标题Re: [问题] 关於运算子多载的问题
时间Wed Sep 28 11:48:33 2005
※ 引述《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