我通过反射调用一个静态方法Parse,因为我不知道编译时对象的类型(但我知道它有一个Parse方法,带一个字符串).
但是,我得到了一个模糊的匹配异常,大概是因为有很多重载的Parse方法,每个方法都占用一个对象(string,int,double等).
如何在我的方法调用中更具体,以确保我到达正确的方法(Parse(string s))并且不抛出异常.
我的代码看起来像这样:
Type returnType = p.PropertyType;
object value = returnType.GetMethod("Parse").Invoke(null, new string[] { "1" });
Run Code Online (Sandbox Code Playgroud) 我正在学习C++中的函数重载,并遇到了这个问题:
void display(int a)
{
cout << "int" << endl;
}
void display(unsigned a)
{
cout << "unsigned" << endl;
}
int main()
{
int i = -2147483648;
cout << i << endl; //will display -2147483648
display(-2147483648);
}
Run Code Online (Sandbox Code Playgroud)
根据我的理解,该int范围内给出的任何值(在我的情况下int是4个字节)都将调用,display(int)并且该范围之外的任何值都将是不明确的(因为编译器无法决定调用哪个函数).int除了最小值之外,它对整个值范围有效,即-2147483648编译因错误而失败
超载的召唤
display(long int)是模棱两可的
但是将相同的值与a int打印并给出值2147483648.我对这种行为感到困惑.
为什么只有在传递最负数时才会观察到这种行为?(该行为是相同的,如果一个short使用具有-32768-事实上,在负数和正数具有相同的二进制表示任何情况下)
使用的编译器:g ++(GCC)4.8.5
我最近修复了一个错误。
在以下代码中,一个重载函数是const,而另一个则不是。通过将两个函数都设为const可以解决此问题。
我的问题是为什么编译器只在参数为0时才抱怨它。
#include <iostream>
#include <string>
class CppSyntaxA
{
public:
void f(int i = 0) const { i++; }
void f(const std::string&){}
};
int main()
{
CppSyntaxA a;
a.f(1); // OK
//a.f(0); //error C2666: 'CppSyntaxA::f': 2 overloads have similar conversions
return 0;
}
Run Code Online (Sandbox Code Playgroud) 根据这个问题,当尝试在不明确的重载构造函数之间进行选择时,Java将选择"最具体"的选项.在这个例子中:
public class Test{
private Test(Map map){
System.out.println("Map");
}
private Test(Object o){
System.out.println("Object");
}
public static void main(String[] args){
new Test(null);
}
}
Run Code Online (Sandbox Code Playgroud)
它会打印出来
"地图"
但是,我试图找出"最具体"的含义.我认为这意味着"最不模糊",如"可能指的是最不可能的类型".在这种情况下,Object可能是任何不是原始的东西,而Map可能只是Map或? extends Map.基本上,我假设选择了接近继承树的叶子的类.当一个类是另一个类的子类时,这是有效的:
public class Test{
private Test(A a){
System.out.println("A");
}
private Test(B b){
System.out.println("B");
}
public static void main(String[] args){
new Test(null);
}
}
class A{}
class B extends A{}
Run Code Online (Sandbox Code Playgroud)
"B"
然后我想出了这个:
public class Test{
private Test(A a){
System.out.println("A");
}
private Test(E e){
System.out.println("E"); …Run Code Online (Sandbox Code Playgroud) 编译以下代码将返回The call is ambiguous between the following methods or properties错误.如何解决它,因为我无法显式转换null为任何这些类?
static void Main(string[] args)
{
Func(null);
}
void Func(Class1 a)
{
}
void Func(Class2 b)
{
}
Run Code Online (Sandbox Code Playgroud) 考虑这个最小的、可重现的例子:
interface Code {
static void main(String[] args) {
symbol(
String.valueOf(
true ? 'a' :
true ? 'b' :
true ? 'c' :
fail()
)
);
}
private static void symbol(String symbol) {
System.out.println(symbol);
}
private static <R> R fail() {
throw null;
}
}
Run Code Online (Sandbox Code Playgroud)
(接近最小,true是一个有用的布尔表达式的替代品。除了第一个之外,我们可以忽略? :(在实际代码中,有很多)。)
这“显然”给出了错误。
4: reference to valueOf is ambiguous
both method valueOf(java.lang.Object) in java.lang.String and method valueOf(char) in java.lang.String match
Run Code Online (Sandbox Code Playgroud)
好的,让我们修复它。这是String.valueOf(Object)我想要的超载 - 我以后可能想添加:
true ? "sss" : …Run Code Online (Sandbox Code Playgroud) 我正在为类编写C++程序,我的编译器抱怨"模糊"函数调用.我怀疑这是因为有几个函数使用不同的参数定义.
如何告诉编译器我想要哪一个?除了特定于案例的修复,是否有一般规则,例如类型转换,可以解决这些问题?
编辑:
在我的情况下,我尝试abs()在一个cout声明中调用,传入两个doubles.
cout << "Amount is:" << abs(amountOrdered-amountPaid);
EDIT2:
我包括这三个标题:
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
Run Code Online (Sandbox Code Playgroud)
EDIT3:
我已经完成了没有这个代码的程序,但为了完成这个问题,我已经重现了这个问题.逐字错误是:
打电话给'abs'是模棱两可的.
编译器提供三个版本abs,每个版本采用不同的数据类型作为参数.
#include <iostream>
using namespace std;
struct test
{
test(){cout<<"class"<<endl;}
};
void test(){cout<<"function"<<endl;}
int main()
{
test();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:
function
Run Code Online (Sandbox Code Playgroud)
(VS2013 ang gcc 4.8.1)
为什么选择功能?是不是模棱两可?
我正在尝试在microsoft C++编译器14.1(Visual Studio 2017)上编译库,但由于对类方法的模糊调用,我得到一个奇怪的错误.经过一些测试后,我分离了以下代码片段:
#include <iostream>
struct Event
{};
template<typename Derived>
struct State
{
public:
template<typename Fsm>
void onEvent(Fsm& fsm, const Event& event)
{
std::cout << "State::onEvent\n";
}
};
struct DerivedState
: State<DerivedState>
{
public:
using State::onEvent;
template<typename Fsm>
void onEvent(Fsm& fsm, const Event& event)
{
std::cout << "DerivedState::onEvent\n";
}
};
struct Context
{};
int main()
{
DerivedState ds;
Context context;
ds.onEvent(context, Event());
}
Run Code Online (Sandbox Code Playgroud)
我得到以下输出:
1> c:\ users\pmas\documents\visual studio
2017\projects\consoleapplication3\consoleapplication3\consoleapplication3.cpp(87): error C2668: 'DerivedState::onEvent': ambiguous call to overloaded function
1>c:\users\pmas\documents\visual studio …Run Code Online (Sandbox Code Playgroud) 我遇到了扩展方法解决方案的问题.LINQ和MoreLINQ包含zip方法,它从4.0版本开始在.NET中出现,并且始终在MoreLINQ库中.但是您不能使用具有良好旧扩展方法语法的实现之一.所以这段代码不会编译
using MoreLinq;
using System.Linq;
var students = new [] { "Mark", "Bob", "David" };
var colors = new [] { "Pink", "Red", "Blue" };
students.Zip(colors, (s, c) => s + c );
Run Code Online (Sandbox Code Playgroud)
错误:
The call is ambiguous between the following methods or properties:
'MoreLinq.MoreEnumerable.Zip<string,string,string>
(System.Collections.Generic.IEnumerable<string>,
System.Collections.Generic.IEnumerable<string>, System.Func<string,string,string>)' and
'System.Linq.Enumerable.Zip<string,string,string>
(System.Collections.Generic.IEnumerable<string>,
System.Collections.Generic.IEnumerable<string>, System.Func<string,string,string>)'
Run Code Online (Sandbox Code Playgroud)
我已经找到了Jon Skeet在这篇文章中为MoreLINQ制作Concat方法的好分辨率,但是我不知道方法的好分辨率.stringzip
注意:您始终可以使用静态方法调用语法,并且一切正常
MoreEnumerable.Zip(students, colors, (s, c) => s + c )
Run Code Online (Sandbox Code Playgroud)
但是错过了扩展语法糖点的一点点.如果您使用LINQ和MoreLINQ调用进行大量数据转换 - …
ambiguous-call ×10
c++ ×5
c# ×3
overloading ×3
java ×2
.net ×1
ambiguity ×1
casting ×1
compiler-bug ×1
crtp ×1
integer ×1
linq ×1
morelinq ×1
null ×1
reflection ×1