Help with certain lines.

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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include <iostream>
#include <string>
#include <vector>



using namespace std;
 
class Person
{
      
public:
       
Person();
~Person(){}
void setName(string a)
{
     name = a;
}      
       
void setAge(int p)
{
     
  age = p;  
    
}
void addAge()
{
     age +=1;
}
void printPerson()
{
     cout << "Name: " << name << " age: " << age << endl;
}
            
       
private:
      
int age;
string name; 
    
};


Person::Person()
{
                
}

class Car
{
public:
       
       Car();
       ~Car();
void setModel(string c)
{
     model = c;
}
void setDriver(Person* p)
{
     *driver = *p;
     
}
void setOwner(Person* p)
{
     *owner = *p;
     
}



private:
        
    string model;      
    Person *driver;
    Person *owner;   
};

int main()
{
    
vector<Person*> people;
//vector<Car> vehicle(100);



string name;
int age;
Person *p1; 
   
cout << "Enter a person or -1 to stop: " << endl;
getline(cin, name);


while(name != "-1")
{
cout <<"Enter Age: " << endl;
cin >> age;
cin.sync();

p1 = new Person;
p1->setName(name);
p1->setAge(age);
people.push_back(p1);



cout << "Enter a person or -1 to stop: " << endl;
getline(cin, name);


}

vector<Person*>::iterator it;

for ( it = people.begin(); it != people.end(); ++it ) {
	      // For each person, print out their info
	      it->printPerson();
	   }





    
    
    
    
    
    
 system("PAUSE");   
    
    
}


Every thing works except this will not compile it was compiling before I was using a vector of pointers and just a vector. Specifically not working now is this line it->printPerson(); Thanks in advance.
1
2
3
4
for ( it = people.begin(); it != people.end(); ++it ) {
	      // For each person, print out their info
	      it->printPerson();
	   }
Last edited on
Why are you using a vector<Person*> instead of just vector<Person>?

But if you do want to use the Person* in the vector then you need to change
it->printPerson();
to
(*it)->printPerson();
To properly print the actual contents of the class.

The assignment tells me to use <Person*> :(

Also thank you.
Last edited on
Topic archived. No new replies allowed.