简体   繁体   English

gcast类型转换后找不到重载函数

[英]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. 这可以在Visual Studio 2008中使用。

However, when I trying to build the same thing in Linux (g++ 4.7.2), it complains 但是,当我尝试在Linux(g ++ 4.7.2)中构建相同的东西时,它抱怨

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? 谁能给我一些关于为什么它可以在VS08中运行的想法,以及为什么它不能在Linux中运行? Is there anyway to make it also work in Linux? 无论如何,它也可以在Linux中工作吗?

The cast to (double) means it is creating a temporary object of type double . 强制转换为(double)表示正在创建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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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