Int to short??
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
2020-04-14
12:17 AM
- last edited on
2021-09-15
12:32 PM
by
Noemi Balogh
I’m struggling this stupid problem for a few hours now.
I need to convert int to short.
When I’m doing it with static_cast or just as (short)varName, the solution goes crazy and I’m getting 35 errors (In classes with no connection to my code of conversion) which looks kind of like jibrish.
How do I convert int to short in api?
Thanks in advance!
Solved! Go to Solution.
- Labels:
-
Add-On (C++)
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
2020-04-14 10:18 AM
Tomer1 wrote:There are two different things going on here. If you're casting an int to a short, something like the following is fine:
I need to convert int to short.
When I’m doing it with static_cast or just as (short)varName, the solution goes crazy and I’m getting 35 errors (In classes with no connection to my code of conversion) which looks kind of like jibrish.
How do I convert int to short in api?
auto s = static_cast<short>(num);Under VS 2017, an int is currently 32 bits and a short is 16 bits. The compiler is warning you that assigning the larger type to the smaller could mean that data is lost. Using static_cast just tells the compiler, "I'm certain that this conversion will always be fine". It's important to be aware of issues like this because they can otherwise be a source of silent, hard-to-find bugs.
The other issue is that the errors you're seeing are
Central Innovation

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
2020-04-14 12:58 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
2020-04-14 01:09 AM
poco2013 wrote:My int is always positive and is in the range of short. It’ll never be bigger then 100.
This Question was asked in Stack overflow and may help:
https://stackoverflow.com/questions/18276225/convert-int-to-short-in-c
Important to notice - when I compiled this code on Mac it worked perfectly just with a warning on data loss of the conversion - now I can’t compile without getting an error said I must convert to short.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
2020-04-14 06:06 AM
I have come across this with a number of warning conversions ,particularly in past examples that will generate warning errors if the warning level is set below 2-3 (forget which).
Something to watch out for, though, probably not the problem here?
Windows 11 - Visual Studio 2022; ArchiCAD 27
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
2020-04-14 10:11 AM
poco2013 wrote:It is the problem. I am getting the warning about the implicit conversion and it is treated as error
Thinking about this, there may be some who are not aware that in VS, the conversion of INT to short is a implicit conversion and should not generate a error. It will, however, generate a warning which will assume to be a error if the warning level is set at minimum.
I have come across this with a number of warning conversions ,particularly in past examples that will generate warning errors if the warning level is set below 2-3 (forget which).
Something to watch out for, though, probably not the problem here?
If I’m changing treat warnings as errors setting to no I’m getting the horrible 35 errors again..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
2020-04-14 10:18 AM
Tomer1 wrote:There are two different things going on here. If you're casting an int to a short, something like the following is fine:
I need to convert int to short.
When I’m doing it with static_cast or just as (short)varName, the solution goes crazy and I’m getting 35 errors (In classes with no connection to my code of conversion) which looks kind of like jibrish.
How do I convert int to short in api?
auto s = static_cast<short>(num);Under VS 2017, an int is currently 32 bits and a short is 16 bits. The compiler is warning you that assigning the larger type to the smaller could mean that data is lost. Using static_cast just tells the compiler, "I'm certain that this conversion will always be fine". It's important to be aware of issues like this because they can otherwise be a source of silent, hard-to-find bugs.
The other issue is that the errors you're seeing are
Central Innovation
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
2020-04-14 10:44 AM
Ralph wrote:I now understand better. Thank you.
Tomer1 wrote:There are two different things going on here. If you're casting an int to a short, something like the following is fine:
I need to convert int to short.
When I’m doing it with static_cast or just as (short)varName, the solution goes crazy and I’m getting 35 errors (In classes with no connection to my code of conversion) which looks kind of like jibrish.
How do I convert int to short in api?auto s = static_cast<short>(num);Under VS 2017, an int is currently 32 bits and a short is 16 bits. The compiler is warning you that assigning the larger type to the smaller could mean that data is lost. Using static_cast just tells the compiler, "I'm certain that this conversion will always be fine". It's important to be aware of issues like this because they can otherwise be a source of silent, hard-to-find bugs.
The other issue is that the errors you're seeing arelinkerrors, not compilation. Linking is the last part of the build process and only happens when the code is successfully compiled. The reason you see them appear after adding static_cast is that you have successfully completed compilation and moved onto linking. At this stage, all the required binaries are linked together in a single product – on optimised build will discard any parts of the binary product that will never be used, Those errors are telling you that certain libraries must also be linked into the product, but it doesn't know where to find them. The place to do this is in the project properties under Linker > Input > Additional Dependencies. You can also add libraries by simply adding to the project in the same way that you add source files. I suggest comparing your project to one of the examples as a good starting point.
I still did not understand what libraries I should add there? The classes I wrote? All the items in this list are .lib files..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
2020-04-14 10:50 AM
Central Innovation