CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    Join Date
    Feb 2006
    Posts
    205

    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

  2. #2
    Join Date
    Nov 2006
    Location
    Essen, Germany
    Posts
    1,344

    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

  3. #3
    Join Date
    Feb 2006
    Posts
    205

    Re: Problem in vector.

    The problem is p does not seem to point to this object.

    *p contains 0xcdcdcd.
    thanx
    swetha

  4. #4
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    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.

  5. #5
    Join Date
    Nov 2006
    Location
    Essen, Germany
    Posts
    1,344

    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

  6. #6
    Join Date
    Feb 2006
    Posts
    205

    Re: Problem in vector.

    Quote 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

  7. #7
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    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.

  8. #8
    Join Date
    Feb 2006
    Posts
    205

    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

  9. #9
    Join Date
    Feb 2006
    Posts
    205

    Re: Problem in vector.

    thanx guys,i got it
    rgds
    swetha

  10. #10
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

  11. #11
    Join Date
    Feb 2006
    Posts
    205

    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

  12. #12
    Join Date
    Nov 2006
    Location
    Essen, Germany
    Posts
    1,344

    Re: Problem in vector.

    Cheers,

    I´m glad to have helped.
    - Guido

  13. #13
    Join Date
    Aug 2000
    Location
    New Jersey
    Posts
    968

    Re: Problem in vector.

    Quote 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/
    David Maisonave
    Author of Policy Based Synchronized Smart Pointer
    http://axter.com/smartptr


    Top ten member of C++ Expert Exchange.
    C++ Topic Area

  14. #14
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    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.

  15. #15
    Join Date
    Aug 2000
    Location
    New Jersey
    Posts
    968

    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.
    David Maisonave
    Author of Policy Based Synchronized Smart Pointer
    http://axter.com/smartptr


    Top ten member of C++ Expert Exchange.
    C++ Topic Area

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured