locked
error C3984: a non-value type cannot have any public data members

    Question

  • Hello community,

    I am new to Visual C++, and trying to learn the rules.  In my project I have a simple class, which is really more of a struct, but with some constructors for ease of use. However I am getting errors with my public members.

    #include "Object.h"
    
    namespace Gaia
    {
    	[Windows::Foundation::Metadata::WebHostHidden]
    	public ref class Message sealed :
    		public Object
    	{
    	public:
    
    		Message() : Source(""), Type(""), Data(nullptr) {}
    		Message(Platform::String^ source, Platform::String^ type) : Source(source), Type(type), Data(nullptr) {}
    		Message(Platform::String^ source, Platform::String^ type, Gaia::Object^ data) : Source(source), Type(type), Data(data) {}
    		Message(Message^ msg) : Source(msg->Source), Type(msg->Type), Data(msg->Data) {}
    
    		virtual Platform::String^ GetType() { return "Message"; }
    
    		Platform::String^ Source;
    		Platform::String^ Type;
    		Gaia::Object^ Data;
    	};
    }

    The following errors are shown:

    Error 1 error C3984: 'Gaia::Message' : a non-value type cannot have any public data members 'Source' e:\misc\gaia\message.h 29 1 Gaia

    Error 2 error C3984: 'Gaia::Message' : a non-value type cannot have any public data members 'Type' e:\misc\gaia\message.h 30 1 Gaia

    Error 3 error C3984: 'Gaia::Message' : a non-value type cannot have any public data members 'Data' e:\misc\gaia\message.h 31 1 Gaia

    How do I get around this?  Am I not allowed to have public members?  What is meant by a "non-value" type?


    Land Of Simeria - http://www.stormsonggames.com/los

    • Moved by Jesse Jiang Tuesday, October 16, 2012 3:01 AM (From:Visual C++ General)
    Monday, October 15, 2012 8:45 PM

Answers

All replies

  • C++/Cx doesn't support public fields, so you need to make these properties instead.  In this case, they can be done as trivial properties:

    property Platform::String^ Source;
    property Platform::String^ Type;
    property Gaia::Object^ Data;

    For details, see Properties: http://msdn.microsoft.com/en-us/library/windows/apps/hh755807.aspx


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Proposed as answer by benjaminki.wp7 Monday, October 15, 2012 9:16 PM
    • Marked as answer by Jesse Jiang Monday, October 22, 2012 3:23 AM
    Monday, October 15, 2012 9:11 PM
  • Thanks Reed.

    I also had to initialize the properties inside my constructors, as properties cannot be initialized in a member list.

    #include "Object.h"
    
    namespace Gaia
    {
    	[Windows::Foundation::Metadata::WebHostHidden]
    	public ref class Message sealed :
    		public Object
    	{
    	public:
    
    		Message() { Source = ""; Type = ""; Data = nullptr; }
    		Message(Platform::String^ source, Platform::String^ type) { Source = source; Type = type; Data = nullptr; }
    		Message(Platform::String^ source, Platform::String^ type, Gaia::Object^ data) { Source = source; Type = type; Data = data; }
    		Message(Message^ msg) { Source = msg->Source; Type = msg->Type; Data = msg->Data; }
    
    		virtual Platform::String^ GetType() { return "Message"; }
    
    		property Platform::String^ Source;
    		property Platform::String^ Type;
    		property Gaia::Object^ Data;
    	};
    }


    Land Of Simeria - http://www.stormsonggames.com/los

    Monday, October 15, 2012 9:17 PM
  • Reading a couple of your posts it sounds like you are coming to C++ and C++/CX at the same time.  There can be some challenges in learning them both together.  Not the least of which is that C++/CX imposes some additional rules and restrictions that C++ does not.  It also has additional features that raw C++ does not.  (Having said that, I would still much rather use C++/CX instead of C++/CLI).

    One thing that I do feel is very important when learning is understanding exactly what applies when authoring a WinRT class versus a native class.  What you attempted to do would have been valid C++.

    Monday, October 15, 2012 9:45 PM
  • Hi,

    I will move this thread to Building Metro style apps with C++ forum. This forum only talk about native codes.

    Best regards,
    Jesse


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us

    Tuesday, October 16, 2012 3:01 AM