加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

windows-runtime – 公共成员的签名包含本机类型

发布时间:2020-12-14 05:44:18 所属栏目:Windows 来源:网络整理
导读:我是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 setAutho
我是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}公共成员的签名包含本机类型.我想这是因为我使用的是std :: string而不是Platform :: String,我该如何解决?

解决方法

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

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

如果您希望将此类设置为public并且具有可在WinRT边界上使用的公共构造函数(以便它可以被C#/ VB / JS使用),那么您需要使用WinRT类型(例如Platform :: String) ).在你的类中,存储类型仍然可以是std :: string(虽然我建议使用std :: wstring,否则你需要进行从宽到窄的转换,因为Platform :: Strings是宽字符串).

要在这两种类型之间进行转换,请使用Platform :: String :: Data()来获取底层的wchar_t *,您可以使用它来构造std :: wstring.类似地,Platform :: String有一个构造函数,它接受一个wchar_t *(你可以从std :: wstring :: c_str()获得).

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读