| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 602 人关注过本帖
标题:这里的运算符重载,为什么老是提示:必须包含类型??
只看楼主 加入收藏
lxsxd
Rank: 5Rank: 5
等 级:贵宾
威 望:14
帖 子:153
专家分:357
注 册:2014-4-15
结帖率:96.15%
收藏
已结贴  问题点数:10 回复次数:2 
这里的运算符重载,为什么老是提示:必须包含类型??
一、题目:这道题目很有趣,运用+运算符重载,完成“苹果+水=苹果汁”
二、我的努力
    我信心满满地用了一个下午的时间,编写了代码。可运行时,老是提示这样的错误,
     :错误    1    二元运算符的参数之一必须是包含类型
namespace 书本例题
{
    class Program
    {
        public class AppleWater  //苹果汁
        {
            public string name;
            public string colour;
            public string form;
            public string taste;
        }
        public class Apple    //苹果
        {
            public string name;
            public string colour;
            public string form;
            public string taste;
            public Apple(string name, string colour, string form, string taste)
            {
                this.name = name;
                this.colour = colour;
                this.form = form;
                this.taste = taste;
            }
            public Apple()
            {
                this.name = "苹果";
                this.colour = "红色";
                this.form = "圆形";
                this.taste = "酸甜";
            }
        }
        public class Water   //水
        {
            public string name;
            public string colour;
            public string form;
            public string taste;
            public Water(string name, string colour, string form, string taste)
            {
                this.name = name;
                this.colour = colour;
                this.form = form;
                this.taste = taste;
            }
            public Water()
            {
                this.name = "水";
                this.colour = "无色";
                this.form = "无形";
                this.taste = "无味";
            }

        }
        public static Program operator +(Apple app, Water wat)   //+运算符重载  //此处提示:错误    1    二元运算符的参数之一必须是包含类型   
        {
            AppleWater appwat = new AppleWater();
            if (app.name == "苹果" && wat.name == "水")
            {
                appwat.name = "苹果汁";
            }
            else
            {
                app.name = "匿名";
            }
            if (app.colour = "红色" || app.name = "青色" && wat.colour = "无色")
            {
                appwat.colour = "橙色";
            }
            else
            {
                appwat.colour = "匿色";
            }
            if (app.form = "圆形" && wat.form = "无形")
            {
                appwat.form = "无形";
            }
            else
            {
                appwat.form = "匿形";
            }
            if (app.taste = "酸甜" && wat.taste = "无味")
            {
                appwat.taste = "酸甜可口";
            }
            else
            {
                appwat.taste = "匿味";
            }
            return appwat;
        }
        public void show()   //显示
        {
            Console.WriteLine("名称:{0}",name);
            Console.WriteLine("颜色:{0}", colour);
            Console.WriteLine("形状:{0}", form);
            Console.WriteLine("味道:{0}", taste);

        }

        static void Main(string[] args)
        {
            Apple app = new Apple();
            app.show();
            Water wat = new Water();
            wat.shoe();
            AppleWater appwat = new AppleWater();
            appwat = app + wat;  //调用+运算符重载
            appwat.show();
         
            Console.ReadKey();
        }
    }
}

三、错误之处
在public static Program operator +(Apple app, Water wat)   此处提示:错误    1    二元运算符的参数之一必须是包含类型

小虾不知道怎么办?请高手大侠帮忙看看,哪里错了?如何纠正?小虾在此有礼了。

[ 本帖最后由 lxsxd 于 2014-8-24 20:40 编辑 ]
搜索更多相关主题的帖子: Apple 苹果汁 public 
2014-08-24 20:39
邓士林
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:淮河河畔
等 级:贵宾
威 望:61
帖 子:2392
专家分:13384
注 册:2013-3-3
收藏
得分:10 
public static Program operator +(Apple app, Water wat)  提示的意思就是声明运算符重载的类(Program),类型与参数不一致,在进行函数重载的时候,要求声明运算符的类的类型与参数类型保持一致。
1、 public static Program operator +(Apple app, Water wat)该函数的类型是Program类型,无法返回  return appwat;此类型返回不对;
2、 if (app.form = "圆形" && wat.form = "无形"),这样写错的好多,是比较运算符==,不是赋值=
3、  public void show()   //显示
        {
            Console.WriteLine("名称:{0}",name);
            Console.WriteLine("颜色:{0}", colour);
            Console.WriteLine("形状:{0}", form);
            Console.WriteLine("味道:{0}", taste);

        }
Program类中没有这几个变量,识别不了的,你除非给他类似于AppleWater的对象,然后用对象去调用,或者在类中定义这样的变量。

Maybe
2014-08-25 08:03
lxsxd
Rank: 5Rank: 5
等 级:贵宾
威 望:14
帖 子:153
专家分:357
注 册:2014-4-15
收藏
得分:0 
感谢信
--------
感谢 邓士林 朋友!

艰难地走在C#自学的路上…………
2014-08-28 20:29
快速回复:这里的运算符重载,为什么老是提示:必须包含类型??
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.040036 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved