将值读入List对象

编程入门 行业动态 更新时间:2024-10-27 20:38:20
将值读入List对象 - 未将对象引用设置为对象的实例(Reading values into a List object - Object reference not set to an instance of an object)

尝试将值加载到List对象中,我得到“对象引用未设置为对象的实例”错误。

(编辑问题以简化它)

MyVec.h列表(仅限要点):

ref class MyVec { public: List<double>^ MyVector; MyVec(void); };

MyVec.cpp列表(仅限要点):

#include "MyVec.h" MyVec::MyVec(void) { }

Form.h列表(仅限要点):

MyVec^ TestVec = gcnew MyVec(); double MyDouble = 1.002; TestVec->MyVector->Add(MyDouble); textBox1->Text = TestVec->MyVector[0].ToString() + "\r\n";

我在尝试将MyDouble分配给TestVec时遇到错误:TestVec-> MyVector-> Add(MyDouble)

在Autos窗口中,它表示(除其他外)TestVec-> MyVector未定义的值

怎么了?

Trying to load values into a List object, I get the "Object reference not set to an instance of an object" error.

(edited question to simplify it)

MyVec.h listing (essentials only):

ref class MyVec { public: List<double>^ MyVector; MyVec(void); };

MyVec.cpp listing (essentials only):

#include "MyVec.h" MyVec::MyVec(void) { }

Form.h listing (essentials only):

MyVec^ TestVec = gcnew MyVec(); double MyDouble = 1.002; TestVec->MyVector->Add(MyDouble); textBox1->Text = TestVec->MyVector[0].ToString() + "\r\n";

I get the error where I try to assign MyDouble to TestVec: TestVec->MyVector->Add(MyDouble)

In the Autos window it says (amongst other things) TestVec->MyVector undefined value

What's wrong?

最满意答案

这个改变应该解决问题:

MyVec::MyVec(void) { MyVector = gcnew List<double>(); }

类成员MyVector是initailly null引用。 您需要在使用之前初始化它,并且MyVec构造函数是适当的位置。

This change should solve the problem:

MyVec::MyVec(void) { MyVector = gcnew List<double>(); }

Class member MyVector is initailly null reference. You need to initialize it before using, and MyVec constructor is appropriate place to do it.

更多推荐

MyDouble,MyVec,TestVec,TestVec->MyVector,电脑培训,计算机培训,IT培训"/> <m

本文发布于:2023-08-05 17:17:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1437355.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对象   List

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!