公共成员的签名包含本机类型

Avr*_*dis 3 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_;

};
Run Code Online (Sandbox Code Playgroud)

当我尝试编译它时,我收到以下错误:

{ctor} signature of public member contains native type.我想这是因为我使用的是std :: string而不是Platform :: String,我该如何解决?

And*_*ich 7

您的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*可用于构造a 的底层std::wstring.同样,Platform::String有一个构造函数,它取一个wchar_t*(你可以得到std::wstring::c_str()).