LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-07-2003, 01:01 AM   #1
Xiangbuilder
Member
 
Registered: Apr 2003
Location: Shandong province China
Distribution: fedora core 1.0
Posts: 206

Rep: Reputation: 30
double conversion


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]#
thank you.
 
Old 10-07-2003, 04:32 AM   #2
nephilim
Member
 
Registered: Aug 2003
Location: Belgium
Distribution: Debian (server), Kubuntu (desktop)
Posts: 248

Rep: Reputation: 30
try to change your int main() to

void main(String[] args)

and see what happens.
 
Old 10-07-2003, 08:09 AM   #3
Xiangbuilder
Member
 
Registered: Apr 2003
Location: Shandong province China
Distribution: fedora core 1.0
Posts: 206

Original Poster
Rep: Reputation: 30
Thank you.
Code:
[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]#
maybe the compiler cause the error. my guess.
 
Old 10-07-2003, 08:25 AM   #4
nephilim
Member
 
Registered: Aug 2003
Location: Belgium
Distribution: Debian (server), Kubuntu (desktop)
Posts: 248

Rep: Reputation: 30
As I said before, you should change the "int" to "void". The compiler expects the method to return an integer, but you don't return anything.
 
Old 10-07-2003, 08:29 AM   #5
dakensta
Member
 
Registered: Jun 2003
Location: SEUK
Distribution: Debian & OS X
Posts: 194

Rep: Reputation: 35
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.

Which is what a cast does.
 
Old 10-07-2003, 08:53 AM   #6
nephilim
Member
 
Registered: Aug 2003
Location: Belgium
Distribution: Debian (server), Kubuntu (desktop)
Posts: 248

Rep: Reputation: 30
Quote:
You have compiled sucessfully,...
Not true. It is true you can overlook the warnings, but not the errors.

As long as the int return type isn't changed, the code will never compile.
 
Old 10-07-2003, 09:53 AM   #7
Xiangbuilder
Member
 
Registered: Apr 2003
Location: Shandong province China
Distribution: fedora core 1.0
Posts: 206

Original Poster
Rep: Reputation: 30
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]#

*** ***

Thank you, nephilim and dakensta.
 
Old 10-07-2003, 09:59 AM   #8
Xiangbuilder
Member
 
Registered: Apr 2003
Location: Shandong province China
Distribution: fedora core 1.0
Posts: 206

Original Poster
Rep: Reputation: 30
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++.

thank you.
 
Old 10-07-2003, 12:02 PM   #9
jinksys
Member
 
Registered: Aug 2003
Location: 63123
Distribution: OpenSuSE/Ubuntu
Posts: 419

Rep: Reputation: 35
The code you posted first,

Code:
#include<iostream>
using namespace std;

int main ()
{   
   double d=0.0;
   int x=d; 
   cout<<"x="<<x<<endl;
}

was correct. As stated, your compiler just warns you of
the type conversion.

If you want a program that compiles clean, with no errors, add a type cast like so:

Code:
#include<iostream>
using namespace std;

int main ()
{   
   double d=0.0;
   int x=(int)d; 
   cout<<"x="<<x<<endl;
}

Last edited by jinksys; 10-07-2003 at 04:13 PM.
 
Old 10-07-2003, 12:34 PM   #10
Mohsen
Member
 
Registered: Feb 2003
Location: Iran
Distribution: Solaris 10
Posts: 201

Rep: Reputation: 30
Quote:
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'.
 
Old 10-07-2003, 01:09 PM   #11
jinksys
Member
 
Registered: Aug 2003
Location: 63123
Distribution: OpenSuSE/Ubuntu
Posts: 419

Rep: Reputation: 35
{Edited}

Last edited by jinksys; 10-07-2003 at 04:15 PM.
 
Old 10-07-2003, 01:14 PM   #12
nephilim
Member
 
Registered: Aug 2003
Location: Belgium
Distribution: Debian (server), Kubuntu (desktop)
Posts: 248

Rep: Reputation: 30
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".
 
Old 10-07-2003, 02:09 PM   #13
jinksys
Member
 
Registered: Aug 2003
Location: 63123
Distribution: OpenSuSE/Ubuntu
Posts: 419

Rep: Reputation: 35
Yeah, that was pretty immature. Accept my apologies.

Last edited by jinksys; 10-07-2003 at 04:16 PM.
 
Old 10-09-2003, 02:52 AM   #14
nephilim
Member
 
Registered: Aug 2003
Location: Belgium
Distribution: Debian (server), Kubuntu (desktop)
Posts: 248

Rep: Reputation: 30
Accepted, reading it all again I feel I overreacted.

No hard feelings then.
 
Old 10-09-2003, 07:53 AM   #15
mr_segfault
Member
 
Registered: Oct 2003
Location: Australia
Distribution: Redhat 9
Posts: 95

Rep: Reputation: 15
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"?

Cheers.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Case conversion in C zaichik Programming 21 09-12-2005 06:55 PM
Double the desktop, not double the fun! bizshop SUSE / openSUSE 3 08-26-2005 12:22 PM
c + type conversion dilberim82 Programming 8 03-08-2005 12:17 PM
c++: float to double conversion ashirazi Programming 6 11-30-2004 04:14 PM
Email conversion sk381 Linux - Software 1 01-25-2004 02:05 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:08 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration