Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2004-04-20
7
Medium Priority
?
1,060 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 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
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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

972 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