ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Here is a program from static_cast sector, chapter 2 of <<thinking in c++>>,
why I can't compile it successfully?
Code:
#include<iostream>
using namespace std;
int main ()
{
double d=0.0;
int x=d;
cout<<"x="<<x<<endl;
}
[root@localhost a_static]# g++ static_cast_i.cpp -o static_cast_i
static_cast_i.cpp: In function `int main()':
static_cast_i.cpp:7: warning: initialization to `int' from `double'
static_cast_i.cpp:7: warning: argument to `int' from `double'
[root@localhost a_static]#
[root@localhost a_static]# g++ static_cast_i.cpp -o static_cast_i
static_cast_i.cpp:4: parse error before `)' token
static_cast_i.cpp:5: `main' must return `int'
static_cast_i.cpp: In function `int main(...)':
static_cast_i.cpp:7: warning: initialization to `int' from `double'
static_cast_i.cpp:7: warning: argument to `int' from `double'
[root@localhost a_static]#
You have compiled sucessfully, those are compiler warnings because you are initialising an integer with a double, with the obvious potential for loss of information, without telling the compiler explicitly that you know what you are doing.
hi, nephilim,
"you should change the "int" to "void"."
I had changed it before I compiled it again.
Code:
#include<iostream>
using namespace std;
void main (String[] args)
{
double d=0.0;
int x=d;
cout<<"x="<<x<<endl;
}
[root@localhost a_static]# g++ static_cast_i.cpp -o static_cast_i
static_cast_i.cpp:4: `String' was not declared in this scope
static_cast_i.cpp:4: parse error before `]' token
static_cast_i.cpp:5: `main' must return `int'
static_cast_i.cpp: In function `int main(...)':
static_cast_i.cpp:7: warning: initialization to `int' from `double'
static_cast_i.cpp:7: warning: argument to `int' from `double'
[root@localhost a_static]#
*** ***
hi, dakensta,
After I compiled the program that I mentioned in my first poist,
although there are some warnning information, but a executable file, static_cast_j had been created.
here is the result when I run it.
[root@localhost a_static]# ./static_cast_j
x=0
[root@localhost a_static]#
hi, nephilim,
"The compiler expects the method to return an integer, but you don't return anything."
I don't think so.
if there is no return comman, the compiler, g++ can consider it as return 0;
besides, a main function must return a integer value instead of void (nothiing), it is new rule of c++.
static_cast_i.cpp: In function `int main()':
static_cast_i.cpp:7: warning: initialization to `int' from `double'
static_cast_i.cpp:7: warning: argument to `int' from `double'
Yes, they are all warnings.
And about "main (String[])", AFAIK there is no String implemented for C++. You are complexing JAVA and C++ . But also in JAVA there is no `String' but `string'.
Mohsen,
You are correct, I was thinking in Java and not in C++. My most humble appologies
But in Java, it definitely is String and not string (String is an object and not a primitive). However, that's not the issue here.
jinksys,
I wasn't smoking anything. Maybe you should grab a dictionary and look up the word "polite".
Is it better (and more portable?), to use the type safe casts like:
Code:
#include<iostream>
using namespace std;
int main ()
{
double d=0.0;
int x=static_cast<int>(d);
cout<<"x="<<x<<endl;
}
I have been led to believe that the new style type safe casts should be preferred to the old C style casts, since the type safe casts (static_cast<>, dynamic_cast<>) allow the compiler to determin if the coversion is safe to perform, where as the C style casts tell the compiler to "believe me, its ok"?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.