Solved

Referencing items in a vector<MyClass*> (EASY!)

Posted on 2004-04-20
7
1,056 Views
Last Modified: 2013-12-14
Hey. Having trouble referencing items in vector of pointers to a custom class. Here's a small test program. VC++ 6.0 btw.

------------------------------------------------------------------------
------------------------------------------------------------------------
#include <iostream>
#include <list>
#include <vector>
using namespace std;

class myClass
{
      public:      int id;
};

list <myClass*> LIST;
vector <myClass*> VECTOR;

list<myClass*>::iterator L_iter;
vector<myClass*>::iterator V_iter;

void main()
{

       // intialize list
      for(int i=0; i<50; i++)
      {
            myClass C;
            C.id=i;
            LIST.push_back(&C);
      }

      // initialize vector
      for(i=0; i<50; i++)
      {
            myClass C;
            C.id=i;
            VECTOR.push_back(&C);
      }

      // attempt to access list contents
      for( L_iter = LIST.begin(); L_iter != LIST.end(); L_iter++ )
      {
            cout << *L_iter.id; // error!
      }
      
      // attempt to access vector contents
      for( V_iter = VECTOR.begin(); V_iter != VECTOR.end(); V_iter++ )
      {
            cout << *V_iter.id;  // error!
      }
}
------------------------------------------------------------------------
------------------------------------------------------------------------

Anyway, Ive tried every combination I can think of, it seems like *V_iter->id should be right, but it throws compiler errors.
0
Comment
Question by:Fippy_Darkpaw
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 8

Accepted Solution

by:
_corey_ earned 50 total points
ID: 10872988
You need to put *V_iter in parenthesis.  (*V_iter)->id
0
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
ID: 10873072
_corey_ is right, you first need to dereference the iterator, then you dereference the pointer that you stored.

There are two  more problems in your program (which VSC++ just ignores):
main has to return an integer, so use
int main()
{

    return 0;
}

And, you are defining the "i" variable in a for loop. The standard says that this variable is only valid in the for loop block, you are however using it again in the following for loop. A good compiler will flag this as error. You can work around this by defining the variable before you enter the first loop, or define the variable again in the second loop (which will generate an error with VSC++).
0
 
LVL 1

Expert Comment

by:hal3000
ID: 10873252
Hi Fippy_Darkpaw,
More important you are referencing "C" outside tthe for blocks. It is not in scope at that point. The declaration should be moved out of the for loops.

You are also using the same name ("C") for both your list and your vector - this will be a problem.

Good luck
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Author Comment

by:Fippy_Darkpaw
ID: 10873551
Thanks. Oh yeah. I forgot to use "new" when creating the list and vector. Here's the fixed version.

------------------------------------------------------------------------------------

#include <iostream>
#include <list>
#include <vector>
using namespace std;

class myClass
{
      public:      int id;
};

list <myClass*> LIST;
vector <myClass*> VECTOR;

list<myClass*>::iterator L_iter;
vector<myClass*>::iterator V_iter;

void main()
{
       // intialize list
      for(int i=0; i<50; i++)
      {
            myClass* C = new myClass;
            C->id=i;
            LIST.push_back(C);
      }

      // initialize vector
      for(i=0; i<50; i++)
      {
            myClass* C = new myClass;
            C->id=i;
            VECTOR.push_back(C);
      }

      // attempt to access list contents
      for( L_iter = LIST.begin(); L_iter != LIST.end(); L_iter++ )
      {
            cout << (*L_iter)->id << endl;
      }
      
      // attempt to access vector contents
      for( V_iter = VECTOR.begin(); V_iter != VECTOR.end(); V_iter++ )
      {
            cout << (*V_iter)->id << endl ;
      }
}

------------------------------------------------------------------------------------------------------
0
 
LVL 30

Expert Comment

by:Axter
ID: 10874161
Why are you using pointers in the first place?
Why not use static type?
list <myClass> LIST;
vector <myClass> VECTOR;

This is safer, easier to read, easier to code, and less potential for bugs.
0
 
LVL 8

Expert Comment

by:_corey_
ID: 10878811
Axter,

  I use pointer lists in my code as needed, but I use a custom class that auto-deletes for me as needed.

Corey
0
 
LVL 30

Expert Comment

by:Axter
ID: 10878921
>> I use pointer lists in my code as needed, but I use a custom class that auto-deletes for me as needed.

Yes, but the questioner's code does not show a need for it.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

631 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question