Using a vector of pointers correctly

This is my current code it does everything I want it to, but I am using an array of object pointers and I would like to use a vector of pointers so I can add to the list instead of have to start with a list size.

I.e. using push back correctly.

I need to know how to declare it and use push back in place of the array I have declared and in my switch statements when I create new objects for the array how I would do that with a vector. I can't seem to get it to work.

here is my code so far.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include <iostream>
#include "CarbonFootPrint.h"
#include "Building.h"
#include "Car.h"
#include "Bicycle.h"
#include "Plane.h"
#include "vector"
#include "string"
using namespace std;


int main()
{
int n =0;
cout <<"Enter ammount of objects you would like to get the carbon foot print of. " << endl;
cin >> n;

 
CarbonFootPrint *list[n]; 

char type;

for (int x = 0; x < n; x++)
{
    
cout << "\nPlease select an object type (1)Building (2)Car (3)Bicycle (4)Plane: ";
cin >> type;

switch  (type)
        {
        case '1':
             {
             string name;
             cout << "Enter Building Name: ";
             cin.ignore(1000, '\n');
             getline(cin,name);
             double electric;
             cout << "Enter Amount of Electric used for the building(kilowatts): ";
             cin >> electric;
             double gas;
             cout << "Enter Amount of gas used for the building(Enter in 1000 BTU's): ";
             cin >> gas;
             list[x] = new Building(name, electric, gas); 
                           
                          
             break;
             }
        case '2':
             {
             string Carname;
             cout << "Enter Car Name: ";
             cin.ignore(1000, '\n');
             getline(cin,Carname);
             double Miles;
             cout << "Enter Amount of Miles driven: ";
             cin >> Miles;
             double Mpg;
             cout << "Enter Fuel efficiencity(MPG): ";
             cin >> Mpg;
             list[x] = new Car(Carname, Miles, Mpg);
             break;
             }
        case '3':
             {
             string Bikename;
             cout << "Enter Bicycle Name: " ;
             cin.ignore(1000, '\n');
             getline(cin,Bikename);
             
             list[x] = new Bicycle(Bikename);
              break;
              }
            
        case '4':
             {
             string Planename;
             cout << "Enter Airline: ";
             cin.ignore(1000, '\n');
             getline(cin,Planename);
             double FlyMiles;
             cout << "Enter Amount of Miles flown: ";
             cin >> FlyMiles;
             
             list[x] = new Plane(Planename,FlyMiles);
             
             break;
             }
            
            } 
            
   
}   



for (int i = 0; i < n; i++)
{
     cout << "\nObjects Name: ";
     list[i]->print();
     cout << " [Carbon foot print] : " << list[i]->getCarbonFP() << endl;
     
}   
    
    
  
    
    
    
cout << endl;    
    
    
    
    
    
system("PAUSE");   
    
    
}
Last edited on
This is how I thought I would do it but I get an error and linked to stl_vector.h saying I cant do this because of an abstract class. However, I can use arrays like this with an abstract class so I'm not sure what is happening.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include <iostream>
#include "CarbonFootPrint.h"
#include "Building.h"
#include "Car.h"
#include "Bicycle.h"
#include "Plane.h"
#include "vector"
#include "string"
using namespace std;


int main()
{
int n =0;
cout <<"Enter ammount of objects you would like to get the carbon foot print of. " << endl;
cin >> n;

 
vector <CarbonFootPrint> *list; 

char type;

for (int x = 0; x < n; x++)
{
    
cout << "\nPlease select an object type (1)Building (2)Car (3)Bicycle (4)Plane: ";
cin >> type;

switch  (type)
        {
        case '1':
             {
             string name;
             cout << "Enter Building Name: ";
             cin.ignore(1000, '\n');
             getline(cin,name);
             double electric;
             cout << "Enter Amount of Electric used for the building(kilowatts): ";
             cin >> electric;
             double gas;
             cout << "Enter Amount of gas used for the building(Enter in 1000 BTU's): ";
             cin >> gas;
             list->push_back(Building(name, electric, gas)); 
                           
                          
             break;
             }
        case '2':
             {
             string Carname;
             cout << "Enter Car Name: ";
             cin.ignore(1000, '\n');
             getline(cin,Carname);
             double Miles;
             cout << "Enter Amount of Miles driven: ";
             cin >> Miles;
             double Mpg;
             cout << "Enter Fuel efficiencity(MPG): ";
             cin >> Mpg;
             list->push_back(Car(Carname, Miles, Mpg));
             break;
             }
        case '3':
             {
             string Bikename;
             cout << "Enter Bicycle Name: " ;
             cin.ignore(1000, '\n');
             getline(cin,Bikename);
             
             list->push_back(Bicycle(Bikename));
              break;
              }
            
        case '4':
             {
             string Planename;
             cout << "Enter Airline: ";
             cin.ignore(1000, '\n');
             getline(cin,Planename);
             double FlyMiles;
             cout << "Enter Amount of Miles flown: ";
             cin >> FlyMiles;
             
             list->push_back(Plane(Planename,FlyMiles));
             
             break;
             }
            
            } 
            
   
}   



for (int i = 0; i < n; i++)
{
     cout << "\nObjects Name: ";
     //list[i]->print();
     //cout << " [Carbon foot print] : " //<<// list[i]->getCarbonFP() << endl;
     
}   
    
    
  
    
    
    
cout << endl;    
    
    
    
    
    
system("PAUSE");   
    
    
}
1
2
std::vector< CarbonFootPrint* > list;
list.push_back( new Plane(Planename,FlyMiles) );
get the error push_back has not been declared.

list.push_back(new Building(name, electric, gas));

Edit: Nvm got it Thanks.
Last edited on
By the way, don't forget to delete
Or better, use a smart pointer.
I am trying to do delete []list; before system pause but it does not work.
I have a virtual deconstructor in my base class I'm not sure if that changes things.

virtual ~CarbonFootPrint()
{
}
- would something like this work?

1
2
3
4
5
6
7
for(int z=0;z<list.size();z++)
{

delete list[z]


}
.
From what I can see, what you have is not going to work.

Why are you declaring a pointer to a vector at line 19?
 
vector <CarbonFootPrint> *list; 

You never allocate that vector.

Did you intend for a vector of pointers?
 
vector <CarbonFootPrint *> list; 

That's not going to work either. Where you do a push_back onto the vector, you're calling push_back with a copy of a CarbonFootPrint, not a pointer.

Why not simply declare your vector as:
 
vector <CarbonFootPrint> list; 

Then your code will work assuming you change all the list-> to list.
> Why not simply declare your vector as:vector <CarbonFootPrint> list;
Because he want to use polymorphism. That's an abstract base class.

> I have a virtual deconstructor in my base class
Good.

You need to delete the elements, as you are doing in delete list[z];
Also, before removing a pointer from the container.

Because that's error prone, checkout smart pointers.
@ne555 - Since the OP didn't post CarbonFootPrint, I missed that it was an Abstract Base Class. Makes sense now.
Topic archived. No new replies allowed.