컴파일러 오류 CS0034
'operator' 연산자가 모호하여 'type1' 및 'type2' 형식의 피연산자에 사용할 수 없습니다.
하나의 연산자가 두 개체에 사용되었고 컴파일러에서 변환을 두 개 이상 발견했습니다. 변환은 고유해야 하므로 캐스트를 만들거나 변환 중 하나를 명시적으로 만들어야 합니다. 자세한 내용은 변환 연산자(C# 프로그래밍 가이드)를 참조하십시오.
다음 샘플에서는 CS0034 오류가 발생하는 경우를 보여 줍니다.
// CS0034.cs
public class A
{
// allows for the conversion of A object to int
public static implicit operator int (A s)
{
return 0;
}
public static implicit operator string (A i)
{
return null;
}
}
public class B
{
public static implicit operator int (B s)
// one way to resolve this CS0034 is to make one conversion explicit
// public static explicit operator int (B s)
{
return 0;
}
public static implicit operator string (B i)
{
return null;
}
public static implicit operator B (string i)
{
return null;
}
public static implicit operator B (int i)
{
return null;
}
}
public class C
{
public static void Main ()
{
A a = new A();
B b = new B();
b = b + a; // CS0034
// another way to resolve this CS0034 is to make a cast
// b = b + (int)a;
}
}
C# 1.1에서는 컴파일러 버그로 인해 int 및 bool로의 암시적 사용자 정의 변환이 있는 클래스를 정의하고 해당 유형의 개체에 비트 AND 연산자(&)를 사용할 수 있었습니다. C# 2.0에서는 컴파일러가 C# 사양 규격에 맞도록 이 버그가 수정되었으므로 다음 코드를 사용하면 CS0034 오류가 발생합니다.
namespace CS0034
{
class TestClass2
{
public void Test()
{
TestClass o1 = new TestClass();
TestClass o2 = new TestClass();
TestClass o3 = o1 & o2; //CS0034
}
}
class TestClass
{
public static implicit operator int(TestClass o)
{
return 1;
}
public static implicit operator TestClass(int v)
{
return new TestClass();
}
public static implicit operator bool(TestClass o)
{
return true;
}
}
}