windows-runtime - 公共(public)成员的签名包含 native 类型

标签 windows-runtime c++-cx

我是 Visual C++ 的新手,我有以下代码:

ref class Book sealed
{
public:
    Book(std::string title,std::string author,int year);
    void setTitle(std::string title);
    std::string getTitle() const;
    int getYear() const;
    void setYear(int year);
    void setAuthor(std::string author_);
    std::string getAuthor() const;

private:
    std::string title_;
    std::string author_;
    int year_;

};

当我尝试编译它时,我收到以下错误:
{ctor} signature of public member contains native type .我想这是因为我使用的是 std::string 而不是 Platform::String,我该如何解决?

最佳答案

您的 ref 类本身未标记为 public,因此您似乎只是在内部(作为源)从其他 C++ 使用此类,而不打算将其发布给其他 WinRT 使用者。

如果是这种情况,您可以将构造函数设置为 internal而不是 public ,它将在此组件中公开,并且在外部不可见。实际上,如果这是您的预期用途,那么它可以只是一个常规的“类”而不是“引用类”。如果您确实希望跨 WinRT 边界使用它但不需要构造函数,则可以将其设为“公共(public)引用类”并将构造函数标记为“内部”。有点取决于你的情况。

如果您希望公开该类(class) 有一个可以跨 WinRT 边界使用的公共(public)构造函数(以便它可以被 C#/VB/JS 使用),那么您需要使用 WinRT 类型(例如 Platform::String )。在您的类(class)中,存储类型仍然可以是 std::string (虽然我推荐使用 std::wstring,否则你需要做宽到窄的转换,因为 Platform::Strings 是宽字符串)。

要在这两种类型之间进行转换,请使用 Platform::String::Data()获得基础 wchar_t*您可以使用它来构造 std::wstring .同样,Platform::String有一个构造函数,它采用 wchar_t* (您可以从 std::wstring::c_str() 获得)。

关于windows-runtime - 公共(public)成员的签名包含 native 类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21968506/

相关文章:

c++ - Windows Phone 中没有 FileIO?

windows-runtime - 我什么时候应该在 C++/CX 中使用 ref 类?

c# - StreamSocket : datareader. LoadAsync 无限等待,即使数据可用

c# - 在 Metro 应用程序中将数组从 javascript 传递到 C#

javascript - 在 Windows 8 Metro HTML5 应用程序中调整图像大小同时保持其质量?

c# - 在 WinRT 中从代码隐藏激活底部 AppBar

c++ - Windows 手机 C++

windows-8 - 使用 MessageWebSocket 的 Windows 8 现代/地铁应用程序的 Socket.IO 文档

c++ - 来自 C++/CX 的设备唯一 ID

c# - 无法绑定(bind)到 Windows 8 Store Apps 中的附加属性