English 中文(简体)
integer automatically converting to double but not float
原标题:

I have a function like below:

void add(int&,float&,float&);

and when I call:

add(1,30,30)

it does not compile.

add(1,30.0,30.0) also does not compile.

It seems that in both cases, it gets implicitly converted to double instead of float.

So, do you suggest that it is better to re-define add as add(int&,double&,double&)? Is there any other way of passing making add(1,30,30) work other than casting 30 with float or assigning like "float x = 30 ; add(1,x,x)" ?

I used to think that the compiler will be able to detect that float is a super-set of integer and so would compile it successfully. Apparently, that is not the case.

Thanks!

最佳回答

Your function takes its parameters by reference, not by value, and you can t pass constant integer/floating-point values by non-const reference. You should either change your function to take its parameters by value, or pass actual variables instead of constants, e.g.:

int x = 1;
float y = 30, z = 30;
add(x, y, z);

You can implicitly cast an int to a float or double, but you cannot implicitly cast an int& to a float&.

问题回答

In your function declaration, you are calling for "pass-by-reference":

void add(int&,float&,float&);

but you are trying to invoke the function using constants. That s the problem.

The upsizing of int to float to double works, but the problem is that you aren t passing variables to add(), so inside add() it can t change them.

Either change add() so it is not passing by reference (drop the &s) or pass variables of the appropriate type.





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签