using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace Ex003_02
{
    class Counter1
    {
        
        public int itsVal;
    }
    class Counter2
    {
        public int itVal;
        
    }
         class Counter
    {
        public int itsValu;        public static Counter operator +(Counter1 t1, Counter2 t2)
        {
            Counter newCounter = new Counter();
            newCounter.itsValu = t1.itsVal + t2.itVal;
            return newCounter;
        }        
        
    }
   
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace Ex003_02
{
    
    class Program
    {
        static void Main(string[] args)
        {
            Counter aa=new Counter();
            Counter1 t1 = new Counter1();
            t1.itsVal = 30;
            Counter2 t2 = new Counter2();
            t2.itVal = 15;
            aa = t1 + t2;
            Console.WriteLine("{0}", aa.itsValu);
            Console.ReadKey();
        }
    }
}为什么会提示这个错?我将重载的运算放到其他类中就可以了,为什么呢?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace Ex003_02
{
    class Counter1
    {
        public static Counter operator +(Counter1 t1, Counter2 t2)
        {
            Counter newCounter = new Counter();
            newCounter.itsValu = t1.itsVal + t2.itVal;
            return newCounter;
        }
        public int itsVal;
    }
    class Counter2
    {
        public int itVal;
        
    }
         class Counter
    {
        public int itsValu;                
        
    }
   
}
实在不能理解。求指点。

解决方案 »

  1.   

    class Counter
        {
            public int itsValu;        public static Counter operator +(Counter1 t1, Counter2 t2)
            {
                Counter newCounter = new Counter();
                newCounter.itsValu = t1.itsVal + t2.itVal;
                return newCounter;
            }        
            
        }
      

  2.   

    public static Counter operator +(Counter1 t1, Counter2 t2)
      

  3.   

    我今天也遇到了这个问题,搞清楚了,
    如:
    public static Counter operator +(Counter1 t1, Counter2 t2)
    就相当于
    Counter1.operator +(t1, t2);就是说只有Counter1类本身才能有自己的+,所以才会提示“二元运算符的参数之一必须是包含类型”。
      

  4.   

    楼上正解...并且还不能在Counter2中添加相同的运算符重载方法