Scope of std::vector

S

Steve

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?
 
G

Guest

Steve said:
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
 
S

Sumit Rajan

Steve said:
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-lite/how-to-post.html#faq-5.8


Regards,
Sumit.
 
J

Jacques Labuschagne

Steve said:
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.
 
S

Sumit Rajan

Steve said:
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-lite/how-to-post.html#faq-5.8


Regards,
Sumit.
 
K

Karl Heinz Buchegger

Steve said:
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.
 
G

Greg

Karl said:
Steve said:
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
 
B

BobR

Greg wrote in message
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'. ]
 
S

Steve

Sumit Rajan said:
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-lite/how-to-post.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;
}
 
S

Sumit Rajan

Steve said:
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.
 
S

Sumit Rajan

Sumit Rajan said:
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.


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


Okay... two interesting members: call_list and campaigns.


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.
 
S

Steve

Sumit Rajan said:
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. :)
 
G

Guest

Steve said:
/* 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 said:
#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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top