dereferencing vector iterators

This is a discussion on dereferencing vector iterators within the C++ Programming forums, part of the General Programming Boards category; I want to dereference a vector iterator that points to a vector of class objects whose members are pointers. I'm ...

  1. #1
    Registered User
    Join Date
    Jun 2008
    Location
    Toronto, Ontario, Canada
    Posts
    17

    dereferencing vector iterators

    I want to dereference a vector iterator that points to a vector of class objects whose members are pointers. I'm using a compound class (class 1) whose members are pointers to another class (class 2). Class 2 has an int member variable, which I want to retrieve. If I retrieve the variable using array indexing, everything works fine. But when I try to dereference the iterator, I get a compiler error (gcc version 4.0.1 (Apple Inc. build 5465).

    Here is the code (example.cpp):

    Code:
    //example.cpp
    #include <iostream>
    #include <vector>
    using namespace std;
    
    class Class1{
    	public:
    		Class1() : num(10) { /***/ }
    		int num;
    		int get_num() { return num; }
    };
    
    class Class2{
    	public:
    		Class2(Class1* C1obj) : C1ptr(C1obj) { /***/ }
    		Class1* C1ptr;
    		Class1* get_C1ptr() { return C1ptr; }
    };
    
    int main() {
       int i;
       Class1* C1p;  //pointer to objects of class Class1
       Class2* C2p;  //pointer to objects of class Class2
       vector<Class1> C1v(5); //vector of 5 Class1 objects (calls constructor)
       vector<Class2*> C2v;   //empty vector of pointers to Class2 objects
       vector<Class2*>::iterator p;
    
       //fill empty vector of type Class2 with pointers to objects whose members are of type Class1
       for(i = 0; i < 5; i++) {
    	C1p = &C1v[i];        
    	C2p = new Class2(C1p);
    	C2v.push_back(C2p);
       }
    
       //THIS WORKS PROPERLY
       for(i = 0; i < 5; i++) {
    	cout << data[i]->get_C1ptr()->get_num() << endl;
       }
    
       //THIS CAUSES ERROR
       for(p = data.begin(); p != data.end(); ++p) {
    	cout << *p->get_C1ptr()->get_num() << endl;
       }
    
       return 0;
    }
    Error message (I've tagged this as code to keep out the smileys caused by punctuation):
    Code:
    example.cpp: In function ‘int main()’:
    example.cpp:37: error: request for member ‘get_C1ptr’ in ‘* p. __gnu_cxx::__normal_iterator<_Iterator, _Container>::operator->
      [with _Iterator = Class2**, _Container = std::vector<Class2*, std::allocator<Class2*> >]()’, which is of non-class type ‘Class2*’
    I could use the array indexing, but I want to understand why the iterator doesn't work. What's the proper way to dereference the iterator here?
    Mike
    Last edited by CornedBee; 08-30-2008 at 02:31 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,803
    What is data?

    The problem might be that *p->get_C1ptr()->get_num() is *(p->get_C1ptr()->get_num()), so you might want to write it as (*p)->get_C1ptr()->get_num().
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2008
    Location
    Toronto, Ontario, Canada
    Posts
    17
    whoops, data should have been C2v! Mistake in cut and paste!
    Mike

  4. #4
    Registered User
    Join Date
    Jun 2008
    Location
    Toronto, Ontario, Canada
    Posts
    17
    Yes, using (*p) fixed it. Thank you!!
    Mike

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-22-2009, 05:03 AM
  2. Replies: 0
    Last Post: 03-20-2008, 07:59 AM
  3. Dereferencing Pointers help
    By Rune Hunter in forum C++ Programming
    Replies: 10
    Last Post: 07-14-2007, 06:43 PM
  4. dereferencing void pointer
    By nkhambal in forum C Programming
    Replies: 4
    Last Post: 04-25-2005, 02:47 AM
  5. Glib and file manipulation
    By unixOZ in forum Linux Programming
    Replies: 1
    Last Post: 03-22-2004, 08:39 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21