|
-
January 8th, 2007, 03:27 AM
#1
Problem in vector.
I have a class pagedetails , i am storing objects of pagedetails in a vector.
I want to access the elements of the vector using an iterator.
Code:
std::vector<Pagedetails*> subcontentlist;
subcontentlist.push_back(objptr->subcontent);
vector<Pagedetails*>::iterator p = subcontentlist.begin();
Here objptr->subcontent returns a Pagedetails object.
p should contain address of the Pagedetails object.
This does not happen. Can anyone help me?
rgds
swetha
-
January 8th, 2007, 03:48 AM
#2
Re: Problem in vector.
In the code snippet you provided p contains the address of a pointer to Pagedetails. You have to dereference the iterator to obtain a pointer to a Pagedetails object.
Code:
std::vector<Pagedetails *> subcontentlist;
subcontentlist.push_back(objptr->subcontent);
vector<Pagedetails*>::iterator p = subcontentlist.begin();
Pagedetails *pd = *p;
pd->something();
Regards,
Guido
- Guido
-
January 8th, 2007, 04:01 AM
#3
Re: Problem in vector.
The problem is p does not seem to point to this object.
*p contains 0xcdcdcd.
thanx
swetha
-
January 8th, 2007, 04:14 AM
#4
Re: Problem in vector.
What does this refer to - subcontent?
Can you post the missing definitions for us to know and then the way you use the vector? How you iterate it? Use CODE tags.
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
-
January 8th, 2007, 04:16 AM
#5
Re: Problem in vector.
I see... please check the following conditions:
1) What does objptr->subcontent return? (is it a valid pointer)
2) What value does subcontentlist[0] return?
Regards,
Guido
- Guido
-
January 8th, 2007, 04:35 AM
#6
Re: Problem in vector.
 Originally Posted by exterminator
What does this refer to - subcontent?
Can you post the missing definitions for us to know and then the way you use the vector? How you iterate it? Use CODE tags.
Pagedetails *subcontent.
subcontent stores the address of a pagedetails object.
i want to store this in a vector.
rgds
swetha
-
January 8th, 2007, 04:59 AM
#7
Re: Problem in vector.
What is objptr? subcontent appear to be a public member which is a pointer to Pagecontent.
There are rather a lot of pointers here.
-
January 8th, 2007, 05:41 AM
#8
Re: Problem in vector.
yes objptr is also a pointer to Pagedetails, I am accessing the member i.e subcontent outside the class. To access subcontent i have declared
objptr.
rgds
swetha
-
January 8th, 2007, 05:52 AM
#9
Re: Problem in vector.
thanx guys,i got it
rgds
swetha
-
January 8th, 2007, 06:06 AM
#10
Re: Problem in vector.
 Originally Posted by swetha.bsharma
thanx guys,i got it
rgds
swetha
And now it remains a mystery.
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
-
January 8th, 2007, 07:27 AM
#11
Re: Problem in vector.
actually the clue was given by GNiewerth (hope the spelling is correct) the content of subcontentlist[0] was empty.
Thanku
rgds
swetha
-
January 8th, 2007, 11:07 AM
#12
Re: Problem in vector.
Cheers,
I´m glad to have helped.
- Guido
-
January 9th, 2007, 01:08 AM
#13
Re: Problem in vector.
 Originally Posted by swetha.bsharma
Code:
std::vector<Pagedetails*> subcontentlist;
subcontentlist.push_back(objptr->subcontent);
vector<Pagedetails*>::iterator p = subcontentlist.begin();
I recommend you not store a raw pointer in a vector.
Is there a particular reason why you think this needs to be a pointer instead of a concrete object.
If you really do need a pointer, then I recommend you use a smart pointer like that in the following link:
http://axter.com/smartptr/
-
January 9th, 2007, 05:37 AM
#14
Re: Problem in vector.
Do you ever post anything else about C++ other than trying to sell your smart pointers?
I gave up the battle of trying to beat boost 5 years ago.
-
January 9th, 2007, 08:23 AM
#15
Re: Problem in vector.
The smart pointer link I post also has links to other smart pointers, and information about smart pointers in general.
So I see it as a good starting point for anyone who wants to use smart pointers, regardless if they decide to use my smart pointer or not.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|