2014-05-01 2 views
0

아래 예가 컴파일되지만 실행 중에 실패하는 이유를 알고 싶습니다. 인터페이스를 구현하는 클래스는 조금 다릅니다. DoSomething 구현은 const int를 취하는 반면, 인터페이스에서는 단지 int입니다. const 정보가 런타임시 무시 될 수 있기 때문에 컴파일러에서 오류를 제공하거나 런타임에서 허용해야합니다.런타임에서 C++/CLI가 실패하고 일치하지 않는 메서드로 컴파일되지 않는 이유

#include "stdafx.h" 

using namespace System; 

public interface class IFancyStuff 
{ 
    void DoSomething(int x); 
}; 

public ref class FancyClass : public IFancyStuff 
{ 
public: 
    virtual void DoSomething(const int x) 
    { 
    Console::WriteLine("hello world"); 
    } 
}; 

int _tmain(int argc, _TCHAR* argv[]) 
{ 
    IFancyStuff ^fc = gcnew FancyClass(); 
    fc->DoSomething(42); 
    return 0; 
} 

이 코드는 컴파일되지만 실행시 실패합니다. 이 실행 오류 :

Unhandled Exception: System.TypeLoadException: Method 'DoSomething' in type 'FancyClass' from assembly 'CppCli, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' does not have an implementation. 
    at wmain(Int32 argc, Char** argv) 
    at _wmainCRTStartup() 

/르네

+0

이 질문 오프 주제는 문제를 진단 할 수있는 충분한 정보가 부족하기 때문에 것으로 보인다해야한다. 문제를 자세히 설명하거나 질문 자체에 최소한의 예를 포함 시키십시오. –

+0

잘못된 행동이라고 생각되는 행동은 무엇이며 왜 다른 사람이 당신을 도울 수 있는지 작성하는 것이 좋습니다 (예 : 실패 방법, 오류 메시지). – Scis

답변

0

당신은 상속의 주위 길을 잘못이있다.

또한 기본 메서드를 호출하는 특수 메서드를 실제로 구현해야합니다.

#include "stdafx.h" 

using namespace System; 


public ref class FancyClass 
{ 
public: 
    virtual void DoSomething(const int x) 
    { 
    Console::WriteLine("hello world"); 
    } 
}; 

public interface class IFancyStuff : public FancyClass 
{ 
    void DoSomething(int x) { 
     FancyClass::DoSomething(x); 
    } 

}; 


int _tmain(int argc, _TCHAR* argv[]) 
{ 
    IFancyStuff ^fc = gcnew FancyClass(); 
    fc->DoSomething(42); 
    return 0; 
} 
관련 문제