473,382 Members | 1,407 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

short res = shortA + shortB result in compiler error. Why?

The following code result in the following compilation error: Error 1
Cannot implicitly convert type 'int' to 'short'. An explicit conversion
exists (are you missing a cast?)

short a = 1;
short b = 2;
short result = a + b;

Can anyone explain why??

Nov 22 '06 #1
10 5409
1 e 2 sono int. Prova con:

short a = (short) 1;
short b = (short) 2;
short result = a + b;
ny***********@gmail.com ha scritto:
The following code result in the following compilation error: Error 1
Cannot implicitly convert type 'int' to 'short'. An explicit conversion
exists (are you missing a cast?)

short a = 1;
short b = 2;
short result = a + b;

Can anyone explain why??
Nov 22 '06 #2

ny***********@gmail.com ha scritto:
The following code result in the following compilation error: Error 1
Cannot implicitly convert type 'int' to 'short'. An explicit conversion
exists (are you missing a cast?)

short a = 1;
short b = 2;
short result = a + b;

Can anyone explain why??
Nov 22 '06 #3
1 e 2 sono interi. Prova con:

short a = (short) 1;
short b = (short) 2;
short result = a + b;

Funziona?

ny***********@gmail.com ha scritto:
The following code result in the following compilation error: Error 1
Cannot implicitly convert type 'int' to 'short'. An explicit conversion
exists (are you missing a cast?)

short a = 1;
short b = 2;
short result = a + b;

Can anyone explain why??
Nov 22 '06 #4

ny***********@gmail.com wrote:
The following code result in the following compilation error: Error 1
Cannot implicitly convert type 'int' to 'short'. An explicit conversion
exists (are you missing a cast?)
You can either use fully grown int's, or do this:
short result = (short)(a + b);

This article explains what's going on:
http://msdn.microsoft.com/library/de...vclrfShort.asp

Nov 22 '06 #5
1 e 2 are integers. Try:

short a = (short) 1;
short b = (short) 2;
short result = a + b;
OK?

ny***********@gmail.com ha scritto:
The following code result in the following compilation error: Error 1
Cannot implicitly convert type 'int' to 'short'. An explicit conversion
exists (are you missing a cast?)

short a = 1;
short b = 2;
short result = a + b;

Can anyone explain why??
Nov 22 '06 #6

Diogene Laerzio wrote:
1 e 2 are integers. Try:

short a = (short) 1;
short b = (short) 2;
short result = a + b;
That doesn't work in my version (2005) because it's the result of the
addition that is an int, not the internal value of a and b.

Nov 22 '06 #7
Diogene Laerzio wrote:
1 e 2 are integers. Try:

short a = (short) 1;
short b = (short) 2;
short result = a + b;
The first two lines were fine before - the compile can tell they're
numeric literals.

The issue is that there's no short operator+ (short, short) - only int
operator+ (int, int).

Basically, the OP needs to cast the result:

short a = 1;
short b = 2;
short result = (short) (a+b);

Jon

Nov 22 '06 #8
I was searching a way to overload the plus operator but I couldn't find
it. Is it possible to define a:

"short operator+ (short, short)" ?

or to redefine:

"int operator+ (int, int)" ?

Jon Skeet [C# MVP] ha scritto:
Diogene Laerzio wrote:
1 e 2 are integers. Try:

short a = (short) 1;
short b = (short) 2;
short result = a + b;

The first two lines were fine before - the compile can tell they're
numeric literals.

The issue is that there's no short operator+ (short, short) - only int
operator+ (int, int).

Basically, the OP needs to cast the result:

short a = 1;
short b = 2;
short result = (short) (a+b);

Jon
Nov 22 '06 #9
The plus operator implicitly up casts the shorts to ints and generates an int
result which can't be implicitly down cast back o a short. So you need to
explicitly cast the sum (short)(a+b). This is like telling the compiler "I
know what I'm doing and it is OK, trust me.".

--
Thanks
Jonny
"ny***********@gmail.com" wrote:
The following code result in the following compilation error: Error 1
Cannot implicitly convert type 'int' to 'short'. An explicit conversion
exists (are you missing a cast?)

short a = 1;
short b = 2;
short result = a + b;

Can anyone explain why??

Nov 22 '06 #10
Diogene Laerzio <di*************@gmail.comwrote:
I was searching a way to overload the plus operator but I couldn't find
it. Is it possible to define a:

"short operator+ (short, short)" ?

or to redefine:

"int operator+ (int, int)" ?
No. You could write your own utility method:

public static short Add (short x, short y)
{
return (short) (x+y);
}

but I think it would be more effort than just including the casts, to
be honest.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 22 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Philipp | last post by:
Hello I'm working on a piece of code which I did not write and it has variables defined as: unsigned short i:13; unsigned short j:13; unsigned short k:3; As I understand the code, this...
99
by: Glen Herrmannsfeldt | last post by:
I was compiling a program written by someone else about six years ago, and widely distributed at the time. It also includes makefiles for many different systems, so I know it has been compiled...
34
by: Andy | last post by:
Hi, Are 1 through 4 defined behaviors in C? unsigned short i; unsigned long li; /* 32-bit wide */ 1. i = 65535 + 3; 2. i = 1 - 3; 3. li = (unsigned long)0xFFFFFFFF + 3; 4. li = 1...
9
by: Keith | last post by:
The bits get twiddled every now and then. Using gcc 3.2 and 3.4. To build: cc -o test test.c -lm ---------------- 8< test.c ------------------------ #include <stdio.h> #include <math.h> ...
8
by: NilsNilsson | last post by:
I wrote this: short s1 = 0; short s2 = 1; short s3 = s1 + s2; And gor this compile error message: Cannot implicitly convert type 'int' to 'short' What is wrong here?
4
by: sd | last post by:
short Total=11; short Index=Total-1; will occurs the following error: error CS0266: Cannot implicitly convert type 'int' to 'short'. An explicit conversion exists (are you missing a cast?) ...
4
by: slougheed | last post by:
I encountered a problem after we had converted our declarations of 'unsigned short int' to uint16_t. In one instance, whoever did the conversion failed to delete the 'short' keyword so we had a...
2
by: akhilesh.noida | last post by:
I am trying to compile glibc-2.5 for ARM based board. But I am getting errors while configuring it. Please check and give your inputs for resolving this. configure command : $...
3
by: mathieu | last post by:
Could someone please tell me what is wrong with the following -ugly- piece of c++ code. Why when I explicititely set the template parameter my gcc compiler start getting confused: bla.cxx: In...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.