std :: vector的范围 [英] Scope of std::vector

查看:79
本文介绍了std :: vector的范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我定义了以下私有对象:


std :: vector< Banana>香蕉;


在我的头文件中。我还添加了一个名为FillVector()的方法,

设置向量的大小并用Banana对象填充它。


另一种方法(getSecondBanana) ())我想访问

香蕉的内容,使用例如


Banana second_banana;

second_banana =香蕉[1 ];


不幸的是,当我尝试这样做时,我的程序崩溃了。然而,当我尝试访问

内容时,我用同样的方法填充向量时,它会起作用* b $ b *。这告诉我,向量的范围仅限于填充它的方法。如何设置这样的

对象的范围,以便它可以可以从班级的任何地方访问吗?

I have defined the following private object:

std::vector<Banana> bananas;

in my header file. I have also added a method called FillVector(), which
sets the size of the vector and fills it with Banana objects.

In another method (getSecondBanana()) I want to access the contents of
bananas, using e.g.

Banana second_banana;
second_banana = bananas[1];

Unfortunately my program crashes when I try to do it like this. However it
*does* work when I fill the vector in the same method as I try to access the
contents of this. This suggests to me that the scope of the vector is
restricted to the method it is filled in. How do I set the scope of such an
object so that it can be accessed from anywhere within the class?

推荐答案

Steve写道:
不幸的是,当我尝试这样做时我的程序崩溃了像这样。然而,当我尝试使用与我尝试访问此内容相同的方法填充向量时,它确实*有效。这告诉我,向量的范围仅限于它所填写的方法。如何设置这样一个对象的范围,以便可以从类中的任何地方访问它?
Unfortunately my program crashes when I try to do it like this. However it
*does* work when I fill the vector in the same method as I try to access the
contents of this. This suggests to me that the scope of the vector is
restricted to the method it is filled in. How do I set the scope of such an
object so that it can be accessed from anywhere within the class?




只要您在使用FillVector之前就没有任何问题

getSecondBanana。发布代码,我们会帮助你进一步帮助。


雅克。



There''s nothing wrong with this as long as you FillVector before you
getSecondBanana. Post the code and we''ll help you out further.

Jacques.




"史蒂夫" < ST *** @ hello.com>在消息新闻中写道:43 ********** @ x-privat.org ...

"Steve" <st***@hello.com> wrote in message news:43**********@x-privat.org...
我已经定义了以下私有对象:

标准::矢量< Banana和GT;香蕉;

在我的头文件中。我还添加了一个名为FillVector()的方法,它设置向量的大小并用香蕉对象填充它。

在另一个方法(getSecondBanana())中我想访问香蕉的内容,使用例如香蕉second_banana;
second_banana =香蕉[1];

不幸的是,当我尝试这样做时,我的程序崩溃了像这样。然而,当我尝试使用相同的方法填充向量时,它确实*有效。我尝试访问它的内容。这告诉我,向量的范围仅限于它所填写的方法。如何设置这样一个对象的范围,以便可以从类中的任何地方访问它?
I have defined the following private object:

std::vector<Banana> bananas;

in my header file. I have also added a method called FillVector(), which
sets the size of the vector and fills it with Banana objects.

In another method (getSecondBanana()) I want to access the contents of
bananas, using e.g.

Banana second_banana;
second_banana = bananas[1];

Unfortunately my program crashes when I try to do it like this. However it
*does* work when I fill the vector in the same method as I try to access
the contents of this. This suggests to me that the scope of the vector is
restricted to the method it is filled in. How do I set the scope of such
an object so that it can be accessed from anywhere within the class?



您能否发布一些证明您问题的代码?请

记得保持最小化和可编译性:
http://www.parashift.com/c++-faq-lit...t.html#faq-5.8

问候,

Sumit。

-

Sumit Rajan< su ********* @ gmail .com>


Could you please post some code that demonstates your problem? Please
remember to keep it minimal and compile-able:
http://www.parashift.com/c++-faq-lit...t.html#faq-5.8
Regards,
Sumit.
--
Sumit Rajan <su*********@gmail.com>




" Steve" < ST *** @ hello.com>在消息新闻中写道:43 ********** @ x-privat.org ...

"Steve" <st***@hello.com> wrote in message news:43**********@x-privat.org...
我已经定义了以下私有对象:

标准::矢量< Banana和GT;香蕉;

在我的头文件中。我还添加了一个名为FillVector()的方法,它设置向量的大小并用香蕉对象填充它。

在另一个方法(getSecondBanana())中我想访问香蕉的内容,使用例如香蕉second_banana;
second_banana =香蕉[1];

不幸的是,当我尝试这样做时,我的程序崩溃了像这样。然而,当我尝试使用相同的方法填充向量时,它确实*有效。我尝试访问它的内容。这告诉我,向量的范围仅限于它所填写的方法。如何设置这样一个对象的范围,以便可以从类中的任何地方访问它?
I have defined the following private object:

std::vector<Banana> bananas;

in my header file. I have also added a method called FillVector(), which
sets the size of the vector and fills it with Banana objects.

In another method (getSecondBanana()) I want to access the contents of
bananas, using e.g.

Banana second_banana;
second_banana = bananas[1];

Unfortunately my program crashes when I try to do it like this. However it
*does* work when I fill the vector in the same method as I try to access
the contents of this. This suggests to me that the scope of the vector is
restricted to the method it is filled in. How do I set the scope of such
an object so that it can be accessed from anywhere within the class?



您能否发布一些证明您问题的代码?请

记得保持最小化和可编译性:
http://www.parashift.com/c++-faq-lit...t.html#faq-5.8

问候,

Sumit。

-

Sumit Rajan< su ********* @ gmail .com>


Could you please post some code that demonstates your problem? Please
remember to keep it minimal and compile-able:
http://www.parashift.com/c++-faq-lit...t.html#faq-5.8
Regards,
Sumit.
--
Sumit Rajan <su*********@gmail.com>


这篇关于std :: vector的范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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