Search code examples
c++windows-phone-8accelerometer

cannot access private member declared in class 'Windows::Devices::Sensors::Accelerometer'


I have the following code:

AccelerometerDelegate.h

#pragma once
#include "pch.h"

using Windows::Devices::Sensors::Accelerometer;
using Windows::Devices::Sensors::AccelerometerReading;

ref class AccelerometerDelegate sealed
{
public:
    double GetX();
    double GetY();
    double GetZ();
    AccelerometerDelegate();
    void GetCurrentReading();
private:
    Accelerometer ^ accelerometer;
    AccelerometerReading ^ accelerometerReading;
};

AccelerometerDelegate.cpp

#include "pch.h"
#include "AccelerometerDelegate.h"

AccelerometerDelegate::AccelerometerDelegate()
{
    accelerometer = ref new Accelerometer();
}

void AccelerometerDelegate::GetCurrentReading()
{
    accelerometerReading = accelerometer->GetCurrentReading();
}

double AccelerometerDelegate::GetX()
{
    return accelerometerReading->AccelerationX;
}

double AccelerometerDelegate::GetY()
{
    return accelerometerReading->AccelerationY;
}

double AccelerometerDelegate::GetZ()
{
    return accelerometerReading->AccelerationZ;
}

The construct declaration is throwing a compile error:

Error   1   error C2248: 'Windows::Devices::Sensors::Accelerometer::Accelerometer' : cannot access private member declared in class 'Windows::Devices::Sensors::Accelerometer'  

I am calling it within my Game class:

Game.h

#pragma once

#include "pch.h"
#include "CubeRenderer.h"
#include "AccelerometerDelegate.h"

ref class Game sealed : public Windows::ApplicationModel::Core::IFrameworkView
{
public:
    Game();
    ...    
private:
    AccelerometerDelegate ^ accelerometer;
    ...
};

Game.cpp

void Game::Initialize(CoreApplicationView^ applicationView)
{
    ...             
    accelerometer = ref new AccelerometerDelegate();
}

Do you know why this might be?

  • Accelerometer is built into a windows phone namespace: Windows::Devices::Sensors
  • Its declaration is as follows:

    public : ref class Accelerometer sealed
        Member of Windows::Devices::Sensors
    
    Summary:
    Represents an accelerometer sensor.
    
    Attributes:
    [Windows::Foundation::Metadata::DualApiPartitionAttribute(version = 100794368),
    Windows::Foundation::Metadata::VersionAttribute]
    

Solution

  • Seems to be a singleton that you have to retrieve using Accelerometer::GetDefault()

    See the documentation.