Solved

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

Posted on 2004-04-20
7
1,049 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

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…
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 be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now