컴파일러 오류 CS0034

‘operator’ 연산자가 모호하여 ‘type1’ 및 ‘type2’ 형식의 피연산자에 사용할 수 없습니다.

두 개체에서 한 연산자가 사용되었으며, 컴파일러에서 둘 이상의 변환을 발견했습니다. 변환은 고유해야 하므로 캐스트를 수행하거나 변환 중 하나를 명시적으로 설정해야 합니다. 자세한 내용은 사용자 정의 변환 연산자를 참조하세요.

다음 샘플에서는 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에서는 컴파일러 버그로 인해 intbool로의 암시적 사용자 정의 변환이 있는 클래스를 정의하고 해당 형식의 개체에 비트 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;
        }
    }

}