相关疑难解决方法(0)

当使用类型提升的重载时,为什么方法调用是模糊的?

public class aman {
    void m(double a , int b, int c) {
        System.out.println("second");
    }
    void m(float a , int b, double c) {
        System.out.println("first");
    }
    public static void main(String[] args) {
        aman obj = new aman();
        obj.m(23, 12, 1);
    }
}
Run Code Online (Sandbox Code Playgroud)

这里,方法m()已经重载但我不理解为什么调用是不明确的,因为在第一种方法中只需要进行1次转换,而在第二种方法中,需要进行两次转换.所以,绝对应该调用第一种方法.请说明为什么没有发生这种情况或者我错过了一些规则.

java

7
推荐指数
1
解决办法
2651
查看次数

编译器如何选择正确的重载函数?

我有一个具有以下构造函数的类:

Color(const float red = 0.0f, const float green = 0.0f, const float blue = 0.0f, const float alpha = 1.0f);
Color(const unsigned char red, const unsigned char green, const unsigned char blue, const unsigned char alpha);
Color(const unsigned long int color);
Run Code Online (Sandbox Code Playgroud)

如果我这样称呼它:

Color c{ 0.0f, 1.0f, 0.0f, 1.0f };
Run Code Online (Sandbox Code Playgroud)

一切都好。但如果我这样称呼它:

Color c{ 78, 180, 84, 255 };
Run Code Online (Sandbox Code Playgroud)

或者

Color c{ 0xffffffff };
Run Code Online (Sandbox Code Playgroud)

我收到

错误 C2668:“Color::Color”:对重载函数的不明确调用

为什么?如何使其正确选择?

c++ overloading

5
推荐指数
1
解决办法
262
查看次数

标签 统计

c++ ×1

java ×1

overloading ×1