3

The following code is illegal ( Visual Studio 2012 Windows Phone( Creating a windows phone direct3d app ) )

a non-value type cannot have any public data members 'posX'

Header

ref class Placement sealed
{
public:
    Placement(
        float rotX, 
        float rotY, 
        float rotZ, 
        float posX, 
        float posY, 
        float posZ
    );
    float rotX, rotY, rotZ, posX, posY, posZ;
};

Cpp

Placement::Placement(
        float rotX, 
        float rotY, 
        float rotZ, 
        float posX, 
        float posY, 
        float posZ
    )
    :   posX( posX ),
        posY( posY ),
        posZ( posZ )
{
    this->rotX = static_cast<float>(rotX);
    this->rotY = static_cast<float>(rotY);
    this->rotZ = static_cast<float>(rotZ);
}

Why and how do I have to set the properties? I'm used to normal C++ not C++ CX( I think it's called that right? )... Do I have to create methods that serve the properties?

*This problem stemmed from me originally trying to create a normal class and creating a pointer to it, only to be complained to about the fact I cannot use * rather I have to use ^ which means I have to create a ref class instead... I don't really understand why?*

Is it something to do with WinRT or more specifically ARM processors?

Jimmyt1988
  • 20,466
  • 41
  • 133
  • 233

2 Answers2

6

Is is a restriction in COM, the underpinning of WinRT and the C++/CX language extension. COM only permits pure virtual methods in interface declarations. A property is fine, that's emulated as a getter and a setter method. Not a field.

This kind of restriction is not artificial, it strongly removes implementation details. Very important when you need to support arbitrary languages and get them to talk to each other or to an API. A field has a very nasty implementation detail, its location is very implementation dependent. Alignment and structure packing rules are important to determine that location and cannot be guaranteed to be compatible between language runtimes.

Using a property is the simple workaround.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
5

This is something specific to WinRT and the C++/CX extensions specifically. C++/CX does not allow ref classes to contain public fields. You need to replace your public fields with public properties.

 ref class Placement sealed
{
public:
    Placement(
        float rotX, 
        float rotY, 
        float rotZ, 
        float posX, 
        float posY, 
        float posZ
    );
    property float rotX;
    property float rotY;
    property float rotZ;
    property float posX;
    property float posY;
    property float posZ;
};

Properties have getter and setter functions generated automatically for them by the compiler.

shf301
  • 31,086
  • 2
  • 52
  • 86
  • So I have to convert all of my C++ classes for the game I created on desktop, to these ref classes instead and change allllll of my fields to properties instead? that's just what I've gotta do kind of thing for games that "may" be used on RT? – Jimmyt1988 Nov 20 '13 at 23:08
  • By the way, I have tried this code and it doesn't like it. It comes with a compile error: error C2144: syntax error : 'float' should be preceded by ';' – Jimmyt1988 Nov 20 '13 at 23:21
  • I had to remove the commas and re-write property float for every single property. weird? – Jimmyt1988 Nov 20 '13 at 23:24
  • 1
    @JamesT - I wasn't sure that the single line declaration was even valid syntax. I've updated my answer so it actually compiles. – shf301 Nov 20 '13 at 23:26
  • Thanks mate, well... there you go... I think i'm only confused at this code because I've only written applications for X86 - Desktop PCs... I've never written for the RT stuff (Which is a different processor right?) – Jimmyt1988 Nov 20 '13 at 23:28
  • @JamesT. It has nothing to do with a different processor. It's code for the Windows Runtime which can run on Desktop PC's as well. – shf301 Nov 20 '13 at 23:34
  • Thanks for clearing that up... except, isn't WinRT designed to work on ARM based processors only? whereas you're more likely to write code for the usual WinAPI which is aimed at X86 processors if you're writing for a desktop pc/x86 laptop/tablet etc? right? – Jimmyt1988 Nov 20 '13 at 23:57
  • Ohhh, no I understand now!!! Microsoft has created this so that you can write code for both, without having to think too much. about doing the correct stuff for whatever platform it's running on... which brings me to understand why C++CX was made.. to work as an interface for "platform-homogenous architecture" – Jimmyt1988 Nov 21 '13 at 00:00