argument for class template

I am struggling to properly use class templates (lines 47, 48) please help. thank you.

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
#include<iostream>
#include<vector>
#include<string>
#include<conio.h>
#include<cstdlib>

using namespace std;

//Person Class

class Person
{
public:
	char name[50];
	int age;
};

// Car class

class Car
{
public:
	char Model[50];
	Person*Owner;
	Person*Driver;

public:
	Car(void)
	{
		Owner=new Person;
		Driver= new Person;
	}
};

// Declarations to iterate person and car vectors

std::vector::iterator it1;
std::vector::iterator it2;

// Declarations of vectors to store person and car info.

vector<char>::iterator PersonVector;
vector<char>::iterator CarVector;

// Person and Car class objects

Person p1;
Car c1;

// Declares and defines function to show person and car details

void DisplayDetails(void)
{
	cout<<"*****Person Details are as follows*****";

	for(it1=PersonVector.begin();it1; it1++)
	{
		cout<<"Name\t"<<it1->name;
		cout<<"Age\t"<<it1->age+1;
	}

	cout<<"*****Car Details are as follow*****";

	for(it2=CarVector.begin();it2;)
	{
		cout<<"Owners Name::"<<it2->Owner->name;
		cout<<"Ownesr Age::"<<it2->Owner->age;
		cout<<"Drivers Name::"<<it2->Driver->name;
		cout<<"Drivers Age::"<<it2->Driver->age;
		cout<<"Car Model::"<<it2->Model;
	}
}

// Defines the function to take user input

void userInput()
{
		cout<<"Enter Person Details:";
		it1=PersonVector.begin();

// This loop will take users Person input starting with index i as 0
	for(int i=0;i<1;i++)
	{
		cout<<"Enter the Person's Name\t";
		cin>>p1.name;
		cout<<"Enter the Person's Age\t";
		cin>>p1.age;

// and inserts the information into the vector.
		PersonVector.push_back(p1);
	}
	
		cout<<"Enter the Car's Details:";
		it2=CarVector.begin();

// This loop will take users Car input starting with index 1 as 0

	for(int i=0; i<1;i++)
	{
		cout<<"Enter the Car's Model\t";
		cin>>c1.Model;
		cout<<"Enter Cars Owner's Name\t";
		cin>>c1.Owner->name;
		cout<<"Enter Car Owner's Age\t";
		cin>>c1.Owner->age;
		cout<<"Enter Car Driver's Name\t";
		cin>>c1.Driver->name;
		cout<<"Enter Car Driver's Age\t";
		cin>>c1.Driver->age;

// Inserts the above values into the vector
		CarVector.push_back(c1);
	}
}

//Begins the main function

int main()
{
	userInput();
	DisplayDetails();
	getch();

	system("pause");
	 return 0;
}
Lines 47 and 48 are simple class objects, not templates.

Lines 24-25, why are Owner and Driver pointers? No need for them to be.
Lines 30-31 not needed if Owner and Driver are not pointers.

You have a memory leak because you never delete Driver and Owner.

Lines 37 and 37, vector is a template. You must specialize it.
1
2
vector<Person>::iterator it1;
vector<Car>::iterator it2;


Lines 42-43, You want vectors of Person and Car respectively, not char, and they aren't iterators.
1
2
vector<Person> PersonVector;
vector<Car> CarVector;


Lines 56, 64, Your for loop are bogus.
1
2
3
4
 
for(it1=PersonVector.begin();it1!=PersonVector.end(); it1++)
...
for(it2=CarVector.begin();it2!=CarVector.end(); it2++)


You should have a display routine for Car and Person. Your loops are 56 and 64 should simply call the class display routine.
1
2
3
4
 
  it1->Display();
...
  it2->Display();


Likewise, each class should have it's own input routine.















Thanks AbstractionAnon. Works great now. I appreciate it.
Topic archived. No new replies allowed.