BIM Coordinator Program (INT) April 22, 2024

Find the next step in your career as a Graphisoft Certified BIM Coordinator!

Archicad C++ API
About Archicad add-on development using the C++ API.
SOLVED!

Int to short??

Anonymous
Not applicable
Hello guys,

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!
1 ACCEPTED SOLUTION

Accepted Solutions
Solution
Ralph Wessel
Mentor
Tomer1 wrote:
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?
There are two different things going on here. If you're casting an int to a short, something like the following is fine:
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 link errors, 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.
Ralph Wessel BArch

View solution in original post

7 REPLIES 7
poco2013
Mentor
This Question was asked in Stack overflow and may help:

https://stackoverflow.com/questions/18276225/convert-int-to-short-in-c
Gerry

Windows 11 - Visual Studio 2022; ArchiCAD 27
Anonymous
Not applicable
poco2013 wrote:
This Question was asked in Stack overflow and may help:

https://stackoverflow.com/questions/18276225/convert-int-to-short-in-c
My int is always positive and is in the range of short. It’ll never be bigger then 100.
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.
poco2013
Mentor
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?
Gerry

Windows 11 - Visual Studio 2022; ArchiCAD 27
Anonymous
Not applicable
poco2013 wrote:
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?
It is the problem. I am getting the warning about the implicit conversion and it is treated as error

If I’m changing treat warnings as errors setting to no I’m getting the horrible 35 errors again..
Solution
Ralph Wessel
Mentor
Tomer1 wrote:
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?
There are two different things going on here. If you're casting an int to a short, something like the following is fine:
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 link errors, 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.
Ralph Wessel BArch
Anonymous
Not applicable
Ralph wrote:
Tomer1 wrote:
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?
There are two different things going on here. If you're casting an int to a short, something like the following is fine:
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 link errors, 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 now understand better. Thank you.
I still did not understand what libraries I should add there? The classes I wrote? All the items in this list are .lib files..
Ralph Wessel
Mentor
Take a look at one of the example projects, e.g. DG_Test. I've attached a screen snapshot of the project structure and highlighted on of the libraries linked with the project. Try adding the same libraries listed in the example to your project.
Ralph Wessel BArch
Learn and get certified!