这个难得是重载时类型转换的错误??

tuhuolongan 2011-05-10 04:19:53
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace csharp_重载
{
class Program
{
static int a(int a1,int a2)
{
return a1-a2;
}
static short a(short b1, short b2)
{
return b1+b2;//无法将int 隐式转换为short 是什么意思 我不是定义b1 b2为short了吗 哪里来的int??
//return (short)(b1+b2);
}
static void Main(string[] args)
{
//double re= Convert.ToDouble(a(15.1,12));
double re=a(15.1,12.2);// 报错说无法double转换int 又是 哪里来的int??
Console.WriteLine(re);
}
}
}

错误1 无法将类型“int”隐式转换为“short”。存在一个显式转换(是否缺少强制转换?) 16
错误2 与“csharp_重载.Program.a(int, int)”最匹配的重载方法具有一些无效参数22
错误3 参数“1”: 无法从“double”转换为“int”22
错误4 参数“2”: 无法从“double”转换为“int”22

路过的大侠指点啊!!!
...全文
75 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
xuexiaodong2009 2011-05-10
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace csharp_重载
{
class Program
{
static int a(int a1,int a2)
{
return a1-a2;
}
static short a(short b1, short b2)
{
return b1+b2;//无法将int 隐式转换为short 是什么意思 我不是定义b1 b2为short了吗 哪里来的int??
//return (short)(b1+b2);
}
static void Main(string[] args)
{
//double re= Convert.ToDouble(a(15.1,12));
double re=a(15.1f,12.2f);// 报错说无法double转换int 又是 哪里来的int??
Console.WriteLine(re);
}
}
}
Snowdust 2011-05-10
  • 打赏
  • 举报
回复
15.1是double,要声明为float在后面加个F,这样就可以了:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace csharp_重载
{
class Program
{
static int a(int a1,int a2)
{
return a1-a2;
}
static float a(float b1,float b2)
{
return b1 + b2;
//return Convert.ToDouble(b1+b2);
}
static void Main(string[] args)
{
//double re= Convert.ToDouble(a(15.1,12));
double re= Convert.ToDouble(a(15.1F,12.2F));
Console.WriteLine(re);
}
}
}
tuhuolongan 2011-05-10
  • 打赏
  • 举报
回复
上面的代码错了 不是short 应该是float
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace csharp_重载
{
class Program
{
static int a(int a1,int a2)
{
return a1-a2;
}
static float a(float b1,float b2)
{
return b1 + b2;
//return Convert.ToDouble(b1+b2);
}
static void Main(string[] args)
{
//double re= Convert.ToDouble(a(15.1,12));
double re= Convert.ToDouble(a(15.1,12.2));
Console.WriteLine(re);
}
}
}
旅行者I号 2011-05-10
  • 打赏
  • 举报
回复
不能将存储大小更大的非文本数值类型隐式转换为 short(有关整型的存储大小的信息,请参见 整型表(C# 参考))。例如,请看以下两个 short 变量 x 和 y:

复制代码
short x = 5, y = 12;


以下赋值语句将产生一个编译错误,原因是赋值运算符右侧的算术表达式在默认情况下的计算结果为 int 类型。

short z = x + y; // Error: no conversion from int to short

若要解决此问题,请使用强制转换:

short z = ( short )(x + y); // OK: explicit conversion

但是,在目标变量具有相同或更大的存储大小时,使用下列语句是可能的:

复制代码
int m = x + y;
long n = x + y;


不存在从浮点型到 short 的隐式转换。例如,除非使用显式强制转换,否则以下语句将生成一个编译器错误:

复制代码
short x = 3.0; // Error: no implicit conversion from double
short y = (short)3.0; // OK: explicit conversion

vrhero 2011-05-10
  • 打赏
  • 举报
回复
C#中,未加声明和类型后缀的数字,无小数默认为int,有小数默认为double...取值范围较大的内置类型不允许隐式转换为取值范围较小的内置类型,必须显式转换...

110,556

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧