컴파일러 오류 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에서는 컴파일러 버그로 인해 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;
}
}
}
GitHub에서 Microsoft와 공동 작업
이 콘텐츠의 원본은 GitHub에서 찾을 수 있으며, 여기서 문제와 끌어오기 요청을 만들고 검토할 수도 있습니다. 자세한 내용은 참여자 가이드를 참조하세요.
.NET
피드백
https://aka.ms/ContentUserFeedback
출시 예정: 2024년 내내 콘텐츠에 대한 피드백 메커니즘으로 GitHub 문제를 단계적으로 폐지하고 이를 새로운 피드백 시스템으로 바꿀 예정입니다. 자세한 내용은 다음을 참조하세요.다음에 대한 사용자 의견 제출 및 보기