Solved

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

Posted on 2004-04-20
7
1,054 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
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
Industry Leaders: 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

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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
debug as  junit test 4 87
is twain_32.dll cmpatible with windows 10 ? 10 226
Error C2678: binary '!=': no operator found... 4 63
print bytes of an integer 6 21
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

713 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