简体   繁体   中英

g++ cannot find the overloaded function after typecast

I have a overloaded function

void FuncGetSomething(string, double&)
void FuncGetSomething(string, int&)
void FuncGetSomething(string, bool&)

.... It is supposed to work this way

double myDbl = 0;
FuncGetSomething("Somename", myDbl) 
//to get the new value for myDbl in side the function, and the new value is preset earlier in the code

But for some reason, I saw someone write this

double myDlb = 0;
FuncGetSomething("Somename", (double)myDbl)

and this works in Visual Studio 2008.

However, when I trying to build the same thing in Linux (g++ 4.7.2), it complains

error: no matching function for call to  GetSomething(const char [8], double) can be found

Can anyone give me some ideas on why it works in VS08 and hwy it is not in Linux? Is there anyway to make it also work in Linux?

The cast to (double) means it is creating a temporary object of type double . When you call the function, you are trying to bind a non-const reference to it, which is not allowed. This might help:

void f( double& ) {};

double d = 1.2;
f( d ); // allowed (1)
f( 1.2 ); // not allowed (2)
f( (double)d ); // not allowed, basically the same as (2)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM