implicit conversion

G

Gary Wessle

Hi

I am getting the warning below.

****************************************************************
cd ~/toy/
make -k
g++ -gdwarf-2 -c -o my.o my.cpp
my.cpp: In function ‘int main()’:
my.cpp:7: warning: converting to ‘int’ from ‘double’
g++ -Wall -gdwarf-2 -o proj my.o -lboost_filesystem -lboost_thread -L.

Compilation finished at Mon Jan 29 08:03:34
****************************************************************

the code is

****************************************************************
#include <iostream>
using namespace std;

int main(){
double b = 0.04;
int a;
a *= b;
cout << a << endl;
}
****************************************************************

do I leave the compiler to do the conversion or should I
static_cast<double> (a) before a *= b?
then
static_cast<int> (a) again when finished?

too much work...

thanks
 
I

Ian Collins

Gary said:
Hi

I am getting the warning below.

****************************************************************
cd ~/toy/
make -k
g++ -gdwarf-2 -c -o my.o my.cpp
my.cpp: In function ‘int main()’:
my.cpp:7: warning: converting to ‘int’ from ‘double’
g++ -Wall -gdwarf-2 -o proj my.o -lboost_filesystem -lboost_thread -L.
****************************************************************
#include <iostream>
using namespace std;

int main(){
double b = 0.04;
int a;
a *= b;
cout << a << endl;
}
****************************************************************

do I leave the compiler to do the conversion or should I
static_cast<double> (a) before a *= b?
then
static_cast<int> (a) again when finished?
The result of the expression a*b will be double, which is implicitly
converted to int on assignment.

Casting won't change that, it'll just hide the compiler warning.
 
I

Ivan Novick

#include <iostream>
using namespace std;

int main(){
double b = 0.04;
int a;
a *= b;
cout << a << endl;}****************************************************************

do I leave the compiler to do the conversion or should I
static_cast<double> (a) before a *= b?
then
static_cast<int> (a) again when finished?

What are you trying to do? a is not initalized, so your program
produces undefined results. If you give us some reasonable code to
look at, we can probably help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top