C++ C++/CLI";无法导入成员“;使用shared_ptr作为参数时发出警告
我在C++/CLI中有以下接口:C++ C++/CLI";无法导入成员“;使用shared_ptr作为参数时发出警告,c++,visual-studio-2010,c++-cli,shared-ptr,C++,Visual Studio 2010,C++ Cli,Shared Ptr,我在C++/CLI中有以下接口: public interface class ISharedPtrInterface { void PrintSharedPtr(std::shared_ptr<std::wstring> ptr); }; 如果运行已编译的方法,则会出现以下运行时错误: 程序集“CLRProject,Version=0.0.0.0,Culture=neutral,PublicKeyToken=null”中类型为“SharedPtrClass”的方法“Pri
public interface class ISharedPtrInterface
{
void PrintSharedPtr(std::shared_ptr<std::wstring> ptr);
};
如果运行已编译的方法,则会出现以下运行时错误:
程序集“CLRProject,Version=0.0.0.0,Culture=neutral,PublicKeyToken=null”中类型为“SharedPtrClass”的方法“PrintSharedPtr”没有实现。
如果在接口/实现中使用直接的std::wstring,则不会发生错误。有人能解释为什么吗
非常感谢 您在公共接口级别混合了本机类型和托管类型。也就是说,在本例中,您有一个公共托管接口方法,该方法将本机类型作为参数。一般来说,这不是一个好主意,因为您无法从C#之类的托管语言轻松使用这些方法,因为您无法提供本机类型 这里的问题与本机类型的可见性有关:默认情况下,所有本机类型都是私有的。当它尝试导入
ISharedPtrInterface::PrintSharedPtr
时,它需要能够访问接口(因为它是公共的),并且能够访问所有参数类型
您可以使用make_public
将本机类型标记为public,或直接将其标记为public(使用/clr编译时)
问题是没有办法使模板类型公开(make_public对它们不起作用)
见:
public ref class SharedPtrClass : public ISharedPtrInterface
{
public:
virtual void PrintSharedPtr(std::shared_ptr<std::wstring> ptr)
{
System::Console::WriteLine(gcnew System::String(ptr->c_str()));
};
};
1>TestSharedPtrInterface.cpp(8): warning C4679: 'ISharedPtrInterface::PrintSharedPtr' : could not import member
1> This diagnostic occurred while importing type 'ISharedPtrInterface ' from assembly 'AnotherCLRProject, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'.