为什么C++/CLI在运行时失败,并且没有为不匹配的方法编译

为什么C++/CLI在运行时失败,并且没有为不匹配的方法编译,c++,c++-cli,C++,C++ Cli,我想知道为什么我下面的示例可以编译,但在运行时失败了?实现接口的类有点不同。DoSomething实现采用常量int,而在接口中它只是一个int。编译器应该提供一个错误,或者运行时应该允许这样做,因为我相信在运行时可以忽略常量信息 #include "stdafx.h" using namespace System; public interface class IFancyStuff { void DoSomething(int x); }; public ref class Fan

我想知道为什么我下面的示例可以编译,但在运行时失败了?实现接口的类有点不同。DoSomething实现采用常量int,而在接口中它只是一个int。编译器应该提供一个错误,或者运行时应该允许这样做,因为我相信在运行时可以忽略常量信息

#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()

/勒内

你的继承方式不对

您还需要实际实现专门的方法来调用基本方法

应该是

#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;
}

这个问题似乎离题了,因为它缺乏足够的信息来诊断问题。更详细地描述你的问题或在问题本身中加入一个最小的例子。最好写下你认为错误的具体行为是什么,以及为什么其他人可能会帮助你,例如它是如何失败的?有错误信息吗?