std::vector as a class data member. Is this wrong?

J

Jerry Krinock

I've declared a class that has some std::vector data members like this:

class MyClass
{
public:
...
std::vector<Apples> apples ;
...
private:
...
std::vector<Oranges> oranges ;
...
}

This program often crashes when I call on my "apples" or "oranges".

Now, I was thinking that maybe the problem is that I declare the instance of
MyClass using "new", so it is on the heap, but I do not use "new" when
declaring the apples and oranges; they look like they are on the stack, yet
they are a member of something which is on the heap. I'm not sure where
they are!

Is there something inherently wrong with the above declaration?

THANKS!

Jerry Krinock
San Jose, CA USA
 
P

Phlip

Jerry said:
I've declared a class that has some std::vector data members like this:

class MyClass
{
public:
...
std::vector<Apple> apples ;
...
private:
...
std::vector<Orange> oranges ;
...
}
Is there something inherently wrong with the above declaration?

What's wrong is apples contains the potential to store Apples, but it has
none.

To push them in, do this:

MyClass aObject;
aObject.apples.push_back(Apple("granny smith"));
aObject.apples.push_back(Apple("crab"));
aObject.apples.push_back(Apple("macintosh"));

Now indices 0, 1 and 2 work in aObject.apples[x]. But before pushing
anything back, any index might crash.

In future, if you report your code crashes, paste in the code that actually
crashes, not just the code that declares data structures associated with the
crash.
 
J

Jonathan Turkanis

Jerry Krinock said:
I've declared a class that has some std::vector data members like this:

class MyClass
{
public:
...
std::vector<Apples> apples ;
...
private:
...
std::vector<Oranges> oranges ;
...
}

This program often crashes when I call on my "apples" or "oranges".

What does 'call on' mean here?
Is there something inherently wrong with the above declaration?

Assuming Apples and Oranges meet the requirements for being stored in
a std::vector (CopyConstructible and Assignable), the only thing that
is (possibly) wrong with the above is that apples is public. But this
won't cause a crash. ;-)

You need to post more code.

Jonathan
 
M

Mark

[..]
Now, I was thinking that maybe the problem is that I declare the instance of
MyClass using "new", so it is on the heap, but I do not use "new" when
declaring the apples and oranges; they look like they are on the stack, yet
they are a member of something which is on the heap. I'm not sure where
they are!

Is there something inherently wrong with the above declaration?

Hard to tell. You might want to post more code. Is apples & oranges
copy constructable and assignable?

Mark
 
J

John Harrison

I've declared a class that has some std::vector data members like this:

class MyClass
{
public:
...
std::vector<Apples> apples ;
...
private:
...
std::vector<Oranges> oranges ;
...
}

This program often crashes when I call on my "apples" or "oranges".

Now, I was thinking that maybe the problem is that I declare the
instance of
MyClass using "new", so it is on the heap, but I do not use "new" when
declaring the apples and oranges; they look like they are on the stack,
yet
they are a member of something which is on the heap. I'm not sure where
they are!

No that's wrong, they are on the heap. The are contained within an object
which you allocated on the heap, therefore they are on the heap.
Is there something inherently wrong with the above declaration?

No, your problem is elsewhere. It's a common thing with newbie posts to
this group, they post the wrong code. To get quick help on this group post
*small* *complete* programs, not snippetts of code. Unfortunately this
seems too difficult for most people to do, so we have to go round the
houses. There are several different possibilites for what is wrong with
your code.

john
 
J

Jerry Krinock

What does 'call on' mean here?

Sorry, I should have said "access" or "read".
Assuming Apples and Oranges meet the requirements for being stored in
a std::vector (CopyConstructible and Assignable),

This might be my problem, because they contain Objective-C objects (it's a
Macintosh program).
the only thing that
is (possibly) wrong with the above is that apples is public.

But this won't cause a crash. ;-)

Yes, I put that public member in there for some sleazy debugging ;).

But before pushing anything back, any index might crash.

I understand that, but, for instance, a push_back() or size() should not
crash, which it does.

...they are on the heap. The are contained within an object
which you allocated on the heap, therefore they are on the heap.
THANKS!


No, your problem is elsewhere.

Thanks very much for all the answers. I know I did not post much code, but
I didn't want to bother everyone with actually fixing the problem. I just
wanted to know if the problem was here or somewhere else. I think I've got
my answer...that it is somewhere else, maybe in my bastardizing of C++ with
Objective-C.

Jerry
 
P

Phlip

Jerry said:
Thanks very much for all the answers. I know I did not post much code, but
I didn't want to bother everyone with actually fixing the problem. I just
wanted to know if the problem was here or somewhere else. I think I've got
my answer...that it is somewhere else, maybe in my bastardizing of C++ with
Objective-C.

Objective-C is two languages trying to become one.
 
J

Jerry Krinock

Yes, it had something to do with the mixing of C++ and Objective-C. I
rewrote the class as an Objective-C class, and also had to make that
std::vector an NSArray, and then the crashes stopped. I still have a
couple other std::vectors in that Objective-C class, but it doesn't
seem to mind that, probably because these other vectors are not
referenced outside the class.

I think I've learned enough. Thanks for all the help,

Jerry
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top