473,403 Members | 2,270 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Scope of std::vector

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?
Oct 14 '05 #1
13 2297
Steve wrote:
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?


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.
Oct 14 '05 #2

"Steve" <st***@hello.com> wrote in message news:43**********@x-privat.org...
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?

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>
Oct 14 '05 #3

"Steve" <st***@hello.com> wrote in message news:43**********@x-privat.org...
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?

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>

Oct 14 '05 #4
Steve wrote:

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?


Your analysis is wrong.
The only thing I can conclude from what you wrote, is:
You may have passed the vector the wrong way: pass per
value instead of pass per reference.

An analogy. In ...

void foo( int i )
{
i = 5;
}

int main()
{
int j = 3;
foo( j );
/// <- Here j still has the value 3, since a copy of j is
/// passed *per value* to foo.
}

.... why does j still have the value 3, when foo attempted to change it
to 5. The answer is: because j is passed per value, a copy of it is passed
to foo. You can change that copy inside foo as often as you like in foo,
that will not impress j.

On the other hand:

void foo( int& i )
{
i = 5;
}
int main()
{
int j = 3;
foo( j );
/// <- Here j has the new value of 5, since it is passed per reference to foo.
/// foo creates a new name for j, inside foo j is known as i. Whatever happens
/// to i, happens to j, since j and i are the same variable.
}

If this is not what you did wrong, then post your code.

--
Karl Heinz Buchegger
kb******@gascad.at
Oct 14 '05 #5
"Sumit Rajan" <su*********@gmail.com> wrote in message
news:3r************@individual.net...

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


OK, I'll post the code when I get home (I don't have access to it here).
Oct 14 '05 #6

Karl Heinz Buchegger wrote:
Steve wrote:

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?


Your analysis is wrong.
The only thing I can conclude from what you wrote, is:
You may have passed the vector the wrong way: pass per
value instead of pass per reference.

....

Well it is also possible that there are simply two different vectors
both named bananas. As usual, having the source would probably help
shed more light on the matter.

Also, could we have a ruling on the legality of "Banana" and "bananas"
as legal C++ identifiers? Based on my knowledge acquired from this
newsgroup, I believe that the only legally recognized identifiers
permitted in a well-formed C++ program are "foo" and "bar".

Greg

Oct 14 '05 #7
"Steve" <st***@hello.com> wrote in message news:43**********@x-privat.org...
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.
You don't show how you're filling the vector. One sure way is

bananas.push_back(my_banana);
bananas.push_back(your_banana);
// etc.
In another method (getSecondBanana()) I want to access the contents of
bananas, using e.g.

Banana second_banana;
second_banana = bananas[1];
You are not accessing the contents of bananas. You are taking a copy of the
second element.
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.
We can only guess: Does Banana need and provide copy constructor, operator=,
and destructor?
This suggests to me that the scope of the vector is restricted to the
method it is filled in.
You don't show but I think bananas is a member of a class. In that case,
bananas will be alive as long as the encapsulating object is alive.
How do I set the scope of such an object so that it can be accessed from
anywhere within the class?


It already happens. You have a bug somewhere else.

Ali

Oct 14 '05 #8

Greg wrote in message
<11*********************@g47g2000cwa.googlegroups. com>...

Also, could we have a ruling on the legality of "Banana" and "bananas"
as legal C++ identifiers? Based on my knowledge acquired from this
newsgroup, I believe that the only legally recognized identifiers
permitted in a well-formed C++ program are "foo" and "bar".
Greg


You are trying to compare apples to oranges!
[ you also forgot 'widgets'. ]

--
Bob '<G>' R
POVrookie
Oct 14 '05 #9
"Sumit Rajan" <su*********@gmail.com> wrote in message
news:3r************@individual.net...

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


I get error Unhandled exception at 0x00415329 in ClientTestClasses.exe:
0xC0000005: Access violation reading location 0x000002c4. It's something to
do with the way I'm filling and/or accessing the vector.

I need to get RequestCall() to return a call from the call_list vector.

/* Manager.cpp */

void Manager::Login()
{
proxy.FillVectors(); /* fills the vector in ClientProxy */
proxy.RequestCall(current_campaign); /* crash occurs in this method */
}

/* ClientProxy.h */
#ifndef __CLIENTPROXY__
#define __CLIENTPROXY__
#include "Call.h"
#include "Campaign.h"
#include "User.h"
#include "vector"
using namespace std;
class ClientProxy

{

public:
ClientProxy();
User Login(const std::string& sname, const std::string& ip, int pn);
Campaign RequestCampaigns(User u);
Call RequestCall(Campaign c);
bool MarshallRequest();

void FillVectors();

private:
/* Test variables below */
Call GetNextCall();
int call_counter;

/* Real vars */
Call current_call;
Campaign current_campaign;
User user;
vector<Campaign> campaigns;
vector<Call> call_list;
};
/* END CLASS DEFINITION ClientProxy */

#endif

/* ClientProxy.cpp */

#include "ClientProxy.h"
#include "Call.h"
#include <vector>

ClientProxy::ClientProxy()
{

}

void ClientProxy::FillVectors()
{
printf("\n-FillVectors()");
vector<Campaign> campaigns(5);
campaigns[0] = Campaign(1, "Camp1");
campaigns[1] = Campaign(2, "Camp2");
campaigns[2] = Campaign(3, "Camp3");
campaigns[3] = Campaign(4, "Camp4");
campaigns[4] = Campaign(5, "Camp5");

vector<Call> call_list(5);
call_list[0] = Call(1,"01215755533","","Dave Jones","Jones
Construction","123 Main Street","","","");
call_list[1] = Call(2,"01215731432","","Steve Martin","Martins
Butchers","10 West Street","","","");
call_list[2] = Call(3,"01315435467","","Phil Babb","BB Insurance","3 North
Street","","","");
call_list[3] = Call(4,"01415772234","","Tony Van Bronkel","GVK","12 South
Street","","","");
call_list[4] = Call(5,"01518126534","","Steve Alabaster","MRM","13 High
Street","","","");
call_counter = 0;

//printf(call_list[0].GetName().c_str());
}

User ClientProxy::Login(const std::string& sname, const std::string& ip, int
pn)
{
user.SetName(sname);
user.SetUserID(1);
//call_list.insert(sname);
return user;
}

Campaign ClientProxy::RequestCampaigns(User u)
{
return current_campaign;
}

Call ClientProxy::RequestCall(Campaign c)
{
//current_call = GetNextCall();

current_call = call_list[3]; /* ERROR BREAKS HERE */
printf("\n");
printf("-ClientProxy.RequestCall()");
printf(current_call.GetName().c_str());
printf("-eof");
return current_call;
}

/* This is a temporary method to retrieve the contents of the call_list
vector */
Call ClientProxy::GetNextCall() {
Call ctemp = call_list[call_counter];
call_counter++;
return ctemp;
}

bool ClientProxy::MarshallRequest()
{
return 1;
}
Oct 15 '05 #10

"Steve" <st***@hello.com> wrote in message
news:kv****************@newsfe3-gui.ntli.net...
"Sumit Rajan" <su*********@gmail.com> wrote in message
news:3r************@individual.net...

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
I get error Unhandled exception at 0x00415329 in ClientTestClasses.exe:
0xC0000005: Access violation reading location 0x000002c4. It's something
to do with the way I'm filling and/or accessing the vector.

I need to get RequestCall() to return a call from the call_list vector.

/* Manager.cpp */

void Manager::Login()
{
proxy.FillVectors(); /* fills the vector in ClientProxy */
proxy.RequestCall(current_campaign); /* crash occurs in this method */
}

The code is not complete, compile-able or minimal. :-)

However, take a look at the comments below and let me know if it fixes your
problem.

/* ClientProxy.h */
#ifndef __CLIENTPROXY__
#define __CLIENTPROXY__
#include "Call.h"
#include "Campaign.h"
#include "User.h"
#include "vector"
using namespace std;
Not a great idea to have a using directive in a header file.

class ClientProxy

{

public:
ClientProxy();
User Login(const std::string& sname, const std::string& ip, int pn);
Campaign RequestCampaigns(User u);
Call RequestCall(Campaign c);
bool MarshallRequest();

void FillVectors();

private:
/* Test variables below */
Call GetNextCall();
int call_counter;

/* Real vars */
Call current_call;
Campaign current_campaign;
User user;
vector<Campaign> campaigns;
vector<Call> call_list;
Okay... two interesting members: call_list and campaigns.
};
/* END CLASS DEFINITION ClientProxy */

#endif

/* ClientProxy.cpp */

#include "ClientProxy.h"
#include "Call.h"
#include <vector>

ClientProxy::ClientProxy()
{

}

void ClientProxy::FillVectors()
{
printf("\n-FillVectors()");
vector<Campaign> campaigns(5);
Now you're defining another vector called "campaigns". Remember that this is
a local one -- only valid here within this function.

//Consider skipping the above line and using vector<>::reserve(). Something
like:
//campaigns.reserve(5);

//Alternatively, you could initialize the vector to the size you want in the
initializer list
//for ClientProxy().
campaigns[0] = Campaign(1, "Camp1");
campaigns[1] = Campaign(2, "Camp2");
campaigns[2] = Campaign(3, "Camp3");
campaigns[3] = Campaign(4, "Camp4");
campaigns[4] = Campaign(5, "Camp5");
And you make changes to your local "campaigns". None of all this affects the
data member by the same name.

vector<Call> call_list(5);


Same problem here. We got a local "call_list" and changes made are only to
the local "call_list".

Regards,
Sumit.
--
Sumit Rajan <su****@msdc.hcltech.com>
Oct 15 '05 #11

"Sumit Rajan" <su*********@gmail.com> wrote in message
news:3r************@individual.net...

"Steve" <st***@hello.com> wrote in message
news:kv****************@newsfe3-gui.ntli.net...
"Sumit Rajan" <su*********@gmail.com> wrote in message
news:3r************@individual.net...

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


I get error Unhandled exception at 0x00415329 in ClientTestClasses.exe:
0xC0000005: Access violation reading location 0x000002c4. It's something
to do with the way I'm filling and/or accessing the vector.

I need to get RequestCall() to return a call from the call_list vector.

/* Manager.cpp */

void Manager::Login()
{
proxy.FillVectors(); /* fills the vector in ClientProxy */
proxy.RequestCall(current_campaign); /* crash occurs in this method */
}

The code is not complete, compile-able or minimal. :-)

However, take a look at the comments below and let me know if it fixes
your problem.

/* ClientProxy.h */
#ifndef __CLIENTPROXY__
#define __CLIENTPROXY__
#include "Call.h"
#include "Campaign.h"
#include "User.h"
#include "vector"
using namespace std;


Not a great idea to have a using directive in a header file.

class ClientProxy

{

public:
ClientProxy();
User Login(const std::string& sname, const std::string& ip, int pn);
Campaign RequestCampaigns(User u);
Call RequestCall(Campaign c);
bool MarshallRequest();

void FillVectors();

private:
/* Test variables below */
Call GetNextCall();
int call_counter;

/* Real vars */
Call current_call;
Campaign current_campaign;
User user;
vector<Campaign> campaigns;
vector<Call> call_list;


Okay... two interesting members: call_list and campaigns.
};
/* END CLASS DEFINITION ClientProxy */

#endif

/* ClientProxy.cpp */

#include "ClientProxy.h"
#include "Call.h"
#include <vector>

ClientProxy::ClientProxy()
{

}

void ClientProxy::FillVectors()
{
printf("\n-FillVectors()");
vector<Campaign> campaigns(5);


Now you're defining another vector called "campaigns". Remember that this
is a local one -- only valid here within this function.

//Consider skipping the above line and using vector<>::reserve().
Something like:
//campaigns.reserve(5);

//Alternatively, you could initialize the vector to the size you want in
the initializer list
//for ClientProxy().


Or you could skip the above line (vector<Campaign> campaigns(5);)
entirely and just use
campaigns.push_back(...);
every time you want to add an element to the vector.

Regards,
Sumit.
--
Sumit Rajan <su****@msdc.hcltech.com>
Oct 15 '05 #12
"Sumit Rajan" <su*********@gmail.com> wrote in message
news:3r************@individual.net...

Or you could skip the above line (vector<Campaign> campaigns(5);)
entirely and just use
campaigns.push_back(...);
every time you want to add an element to the vector.


Thanks Sumit, it works a treat. :)
Oct 15 '05 #13
"Steve" <st***@hello.com> wrote in message
news:kv****************@newsfe3-gui.ntli.net...
"Sumit Rajan" <su*********@gmail.com> wrote in message
news:3r************@individual.net... /* ClientProxy.h */ #ifndef __CLIENTPROXY__
#define __CLIENTPROXY__
__CLIENTPROXY__ is a reserved name. Quoting from the standard with my
formatting:

<quote>
17.4.3.1.2 Global names

1 Certain sets of names and function signatures are always reserved to the
implementation:

-- Each name that contains a double underscore (_ _) or begins with an
underscore followed by an uppercase letter (2.11) is reserved to the
implementation for any use.

-- Each name that begins with an underscore is reserved to the
implementation for use as a name in the global namespace.165)

[...]

Footnote 165) Such names are also reserved in namespace ::std (17.4.3.1).
</quote>

[...]

Also, you included "vector" in ClientProxy.h:
#include "vector"
[...]
/* ClientProxy.cpp */

#include "ClientProxy.h"
#include "Call.h"
Then you included <vector> in ClientProxy.cpp
#include <vector>


Though I doubt that it has anything to do with your problems, those two
headers are potentially different, because the rules for finding headers
(e.g. searching in directory paths) are different between "" and <> headers.

Ali

Oct 17 '05 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

27
by: Jason Heyes | last post by:
To my understanding, std::vector does not use reference counting to avoid the overhead of copying and initialisation. Where can I get a reference counted implementation of std::vector? Thanks.
18
by: Janina Kramer | last post by:
hi ng, i'm working on a multiplayer game for a variable number of players and on the client side, i'm using a std::vector<CPlayer> to store informatik about the players. CPlayer is a class that...
20
by: Anonymous | last post by:
Is there a non-brute force method of doing this? transform() looked likely but had no predefined function object. std::vector<double> src; std::vector<int> dest; ...
17
by: Michael Hopkins | last post by:
Hi all I want to create a std::vector that goes from 1 to n instead of 0 to n-1. The only change this will have is in loops and when the vector returns positions of elements etc. I am calling...
8
by: Ross A. Finlayson | last post by:
I'm trying to write some C code, but I want to use C++'s std::vector. Indeed, if the code is compiled as C++, I want the container to actually be std::vector, in this case of a collection of value...
32
by: zl2k | last post by:
hi, c++ user Suppose I constructed a large array and put it in the std::vector in a function and now I want to return it back to where the function is called. I can do like this: ...
56
by: Peter Olcott | last post by:
I am trying to refer to the same std::vector in a class by two different names, I tried a union, and I tried a reference, I can't seem to get the syntax right. Can anyone please help? Thanks
9
by: aaragon | last post by:
I am trying to create a vector of type T and everything goes fine until I try to iterate over it. For some reason, the compiler gives me an error when I declare std::vector<T>::iterator iter;...
13
by: jubelbrus | last post by:
Hi I'm trying to do the following. #include <vector> #include <boost/thread/mutex.hpp> #include <boost/shared_ptr.hpp> #include <boost/tuple/tuple.hpp> class {
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.