繁体   English   中英

读取访问冲突,这是nullptr错误,未正确初始化?

[英]Read Access Violation this was nullptr error, initilized incorrectly?

我正在使用通用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 nullptrException thrown: read access violation this was nullptr d.meas_v->Append("dk");上发生了Exception thrown: read access violation this was nullptr d.meas_v->Append("dk");

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM