Creating a vector containing a pointer to class

Pages: 12
This is actually some lab work for a few weeks further into my course and I am just working ahead so that I am somewhat familiar with the concepts involved when we do get this problem in class. Thank you for your help anyway seymore it is most appreciated.

I have created an array of products called P[]. The class product has a method called getProducts() which allows me to enter the details of a product.

I then place this array of products into a vector of products know as product list. How can i assign the variables of the products entered to go directly into the vector so that I will not need to use an array at all.

1
2
3
4
5
6
7
8
9
10
11
12
13
main()
{
    product P[3];  //object of type product
    order O[3]; // object of type order

    for(int i=0;i<3;i++)
    {
      cout << "\nEnter Product " << (i+1) << " Information";
      cout << "\n---------------------------\n";
      P[i].getproducts();
    }

    std::vector<product> productList(P, P + sizeof(P)/sizeof(product));
for( int i = 0; i < 3; ++i )
{
productList.push_back( product() );
productList.back().getproducts();
}
Hey there jsmith. I also had another method below this to display the products in the array and my code was previously:

1
2
3
4
5
6
for(int i=0;i<productList.size();i++)
   {
      cout << "\n\nProduct P" << (i+1);
      cout << "\n---------\n";
      P[i].displayproducts();
   }


How could i retrieve and display the information for each product from the productList? would i need to iterate through each element.
If P where a std::vector<product> then the code you have should work just fine since vector<> implements operator[] to work just like arrays.

Or you could use a vector::const_iterator. Consider:

1
2
3
std::vector<int> v;  // Assume this has stuff in it
for( std::vector<int>::const_iterator i = v.begin(); i != v.end(); ++i )
    cout << *i << endl;


would print the entire vector, one integer per line.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

#include <iostream.h>
#include <fstream.h>
#include <iomanip.h>
#include <string.h>
#include <conio.h>
#include <dos.h>
#include <ctype.h>
#include <vector.h>
#include "derived.h"

main()
{
   std::vector<product> p;

 	p.push_back( product("Sony PSP",1001,99.99) );
   p.push_back( product("Sony PS3",1002,299.99) );
   p.push_back( product("Xbox",1003,79.99) );
   p.push_back( product("Xbox 360",1004,179.99) );

   for( std::vector<product>::const_iterator i = p.begin(); i != p.end(); ++i )
    cout << i << endl;
}


I have tried to implement the products to the vector using the following method but have been unable to check to see if they have been initialized as I am getting an illegal structure operation on the pointer to the iterator.

I would be very grateful if you could let me know where I am going wrong as I need to be able to write the vector to a DAT file tommorrow and I have not even gotten to the I/O phase yet
Last edited on
Can someone please help me with how i can iterate through this vector as I am encountering an error stating illegal structure operation
cout << *i << endl;

(note asterisk)

i is the iterator
*i is the element "pointed to" by the iterator.

it does not allow *i either, it still states it is an illegal structure operation
Can you post the actual compile error and the declaration of product?
Topic archived. No new replies allowed.
Pages: 12