如何在另一个类中访问结构类型2d数组 [英] How to access struct type 2d Array in another class

查看:97
本文介绍了如何在另一个类中访问结构类型2d数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经从一个struct创建了一个2D数组,下面是我的代码.

Hi, i have created a 2D array from a struct and below is my code.

class RangeValues
{

public:
    
    
     struct RangeStruct{

        int LabelNo;
        double MinValue;
        double MaxValue;
    };
RangeValues(void);
~RangeValues(void);
RangeStruct MyRangeArray[2][88];
};




我已经在RangeValues.cpp类中初始化了值,并且效果很好...

现在,我想从其他类访问MyRangeArray..




i have initialized values in RangeValues.cpp class and it works fine...

Now i want to access MyRangeArray from a different class..

RangeValues range;
double LabelValue = range.MyRangeArray[0][1].LabelNo;



但是,当我打印该值时,它会给出一个很大的负数.

例如:



But when i print the value it gives something like a big negative number.

ex:

LabelValue  -858993460.00000000 



但是它应该等于1..(MyRangeArray [0] [1] .LabelNo包含数字1)

我的代码有什么问题?谢谢..



But it shoul equals to 1..(MyRangeArray[0][1].LabelNo contains number 1)

What is wrong with my code? Thank you..

推荐答案

您可以按原样使用代码.您正在处理堆栈上的未初始化数据.您应该为您的结构添加一个构造函数,并将该结构中的所有值初始化为某种值.当您在堆栈上实例化类时,该数组将具有一致的值.
You can use your code pretty much as is. You are dealing with uninitialized data on the stack. You should add a constructor for your struct, and initialize all values in the struct to something. When you instantiate your class on the stack, the array will have consistent values.


这篇关于如何在另一个类中访问结构类型2d数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆