g ++在一个* expected *错误消息中给我奇怪的函数调用 [英] g++ gives me strange function call in an *expected* error message

查看:222
本文介绍了g ++在一个* expected *错误消息中给我奇怪的函数调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是情况:

void funct( unsigned u, double d, float f )
{
        u = 12;
}
void funct( double u, int d, void* asd, float f )
{
        u = 13;
}

int main()
{
        const unsigned u = 123;
        double d = 123.123;
        float f = 123.123;

        funct( u, d, f, 123 );

        return 0;
}

给我:

./src/test.cpp:19: error: no matching function for call to 'funct(const unsigned int&, double&, float&, int)'
./src/test.cpp:4: note: candidates are: void funct(unsigned int, double, float)
./src/test.cpp:8: note:                 void funct(double, int, void*, float)

这是绝对预期的错误,因为没有合适的函数被调用,OK,罚款。但是看看编译器的错误:

It's absolutely expected error, as there's no suitable function to be called, OK, fine. But take a look at the compiler error:

                                                          V        V       V
no matching function for call to 'funct(const unsigned int&, double&, float&, int)

为什么这些 有吗?

使用 Ubuntu 10.04

Using Ubuntu 10.04, 64bit and g++ version 4.4.3

推荐答案

您向该函数传递了一个可分配给( lvalue )的实变量。当然,你通过它的值,而不是通过引用 - 但要点是,在你的函数调用你也能够通过引用,因为它是一个 lvalue

You're passing a real variable which can be assigned to (an "lvalue") to the function. Of course you pass it by value, not by reference - but the point is, in your function call you would be able to also pass it by reference, because it's an lvalue.

然后:如果你有一个类型 int& $ c> lvalue ),您可以将其发送到接受 int rvalue lvalue ) - 只是没有反过来。

Then: If you have a value of type int& (lvalue), you are allowed to send it to a function which accepts int (either rvalue or lvalue) - just not the other way round.

这篇关于g ++在一个* expected *错误消息中给我奇怪的函数调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆