Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2004-04-20
7
Medium Priority
?
1,059 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 200 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
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…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

730 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