Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

signature of public member contains native type

I am new to visual c++, I have the following code:

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_;

};

When I am trying to compile it I am getting the following error:

{ctor} signature of public member contains native type. I suppose this is because I am using an the std::string and not the Platform::String, how can I fix that?

like image 200
Avraam Mavridis Avatar asked Feb 14 '23 17:02

Avraam Mavridis


1 Answers

Your ref class is not marked public itself, so it appears you are only consuming this class internally (as source) from other C++, and not intending for it to be published to other WinRT consumers.

If this is the case, you can set your constructor as internal instead of public, which will be public within this component and not visible externally. And really if that's your intended usage, then it can just be a regular 'class' instead of a 'ref class'. If you do wish to use it across the WinRT boundary but you don't need the constructor, you can make it a 'public ref class' and have the constructor marked as 'internal'. Kinda depends on your scenario.

If you instead wish to make this class public and have a public constructor which is usable across the WinRT boundary (so that it can be consumed by C#/VB/JS), then you need to use WinRT types (such as Platform::String). Within your class the storage type can still be a std::string (although I recommend using std::wstring, otherwise you need to do wide-to-narrow conversions, as Platform::Strings are wide strings).

To convert between these two types, use Platform::String::Data() to get at the underlying wchar_t* which you can use to construct a std::wstring. And similarly, Platform::String has a constructor which takes a wchar_t* (which you can get from std::wstring::c_str()).

like image 93
Andy Rich Avatar answered May 10 '23 02:05

Andy Rich