Thread: Access via iterator to pointer container

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    1

    Access via iterator to pointer container

    I am a c++ newbie and I am trying something very similar. Here's my code -

    Code:
    class A
    {
    protected:
      Pointer * p;
      double d;
    };
    
    class B:public A
    {
    .
    .
    }
    
    int main()
    {
     for (list<B *>::const_iterator it = listeB_.begin(); it!=listeB_.end(); it++)
            {
                    cout << (it->p)->getNumero() << endl; 
    }
    The error is 'request for member 'p' in ...., which is of non-class type ....

    Basically, I need to print the value for p. Where am I going wrong?

    Thanks for your help in advance.

    Raj

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    it is a iterator over a list<B *>, therefore if you dereference it (which you do with the -> notation), you get a ... B*, and not a B. Therefore the left hand side is not a class, and therefore any attempt to access a member will fail miserably. You will need to take it, dereference it once to get a B*, and then again to get a B; so something like (*it)-> ....

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Your current problem doesn't actually have anything to do with the other thread, so I split this off. But even if it did have something to do, you should create a new thread and link the old one instead of taking over.

    Your first problem is that you have an iterator over a container holding pointers. Let's keep in mind that a->b is supposed to be semantically equivalent to (*a).b
    What is the type of (*it) in your example? Because the container is of B*, the type of (*it) is also B*, on which you can't use the dot operator. To access the members of B, you need to do (*it)->member.

    But that doesn't resolve your other problem. You're outside B. You have no access rights to B's non-public members, no matter where they come from. If you need that access, you have a design problem, and we'll need more information to help you with that.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  2. Replies: 1
    Last Post: 03-24-2008, 10:16 AM
  3. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. Pointer of arrays - how do I access their elements?
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 10-11-2001, 11:28 PM