读取访问冲突这是nullptr错误,启动不正确?

时间:2017-08-04 13:25:46

标签: c++ visual-studio vector uwp c++-cx

我正在使用通用Windows平台(UWP)在C ++ / CX中编写程序。我是编程新手,对常规C ++更熟悉。这是我第一次制作UWP应用程序。

该程序包含一个名为Dress_pack的类,我在MainPage.xaml.cpp文件中创建了一个名为d的实例。

Dress_pack d ("","","");

Dress_pack有一个名为meas_v的成员,它是Platform :: String ^类型的IVector。这是我的Dress_pack类构造函数:

Dress_pack::Dress_pack(Platform::String^ plant = "", Platform::String^    prog = "", Platform::String^ robot = ""){
plant_no = plant; 
prog_no = prog;
robot_no = robot;
Windows::Foundation::Collections::IVector<Platform::String^>^ meas_v = ref new Platform::Collections::Vector<Platform::String^>();
Windows::Foundation::Collections::IVector<Platform::String^>^ iss_dis_v = ref new Platform::Collections::Vector<Platform::String^>();}

稍后在我的MainPage.xaml.cpp文件中,我尝试将一个字符串附加到此向量,如下所示:

#include "MainPage.xaml.h"
#include "dress_pack.h"
Dress_pack d ("","","");

MainPage::MainPage()
{
InitializeComponent();
d.meas_v->Append("dk");
}

我的MainPage.xaml.h看起来像:

#pragma once

#include "MainPage.g.h"

namespace Project_1
{

public ref class MainPage sealed
{
public:
    MainPage();

};  

}

我的Dress_pack标题包含:

#pragma once
ref class Dress_pack sealed
{

public:
    property Platform::String^ plant_no;
    property Platform::String^ prog_no;
    property Platform::String^ robot_no;
    property Windows::Foundation::Collections::IVector<Platform::String^>^ meas_v; property Windows::Foundation::Collections::IVector<Platform::String^>^ iss_dis_v;

//constructor
Dress_pack(Platform::String^ plant, Platform::String^ prog, Platform::String^ robot);

};

但在运行时,错误Exception thrown: read access violation this was nullptr发生在d.meas_v->Append("dk");

在调试期间,我可以看到meas_v包含值nullptr,它似乎是问题的根源。我不知道为什么会这样。我是否错误地初始化了矢量?还有其他问题吗?向正确的方向推进会有所帮助。如果您需要我发布更多代码来了解问题,请告诉我。谢谢!

1 个答案:

答案 0 :(得分:1)

在构造函数中,您将创建两个恰好与您的字段具有相同名称的新局部变量。

此外,您应该使用L"bla" Platform::String字符串(L前缀强制使用UCS2)。

相关问题