I am trying to create an Array List of Person objects in C++ for a project I have. I am new to programming in C++ so I'm not really sure where to begin. The program builds successfully but I get a strange thread error at the line where I insert a person object into index 0. Could someone please point me in the right direction of how to insert objects into an arraylist? Thank you!
Here is my Person class:
#include <iostream>
using namespace std;
class Person
{
public:
string fName;
string lName;
string hometown;
string month;
int day;
Person();
Person(string f, string l, string h, string m, int d);
void print();
int compareName(Person p);
};
Person::Person(string f, string l, string h, string m, int d) {
fName = f;
lName = l;
hometown = h;
month = m;
day = d;
}
void Person::print() {
std::cout << "Name: " << lName << ", " << fName <<"\n";
std::cout << "Hometown: " << hometown <<"\n";
std::cout << "Birthday: " << month << " " << day <<"\n";
}
ArrayList.h
#ifndef __Project2__ArrayList__
#define __Project2__ArrayList__
#include <iostream>
#include "Person.h"
class ArrayList {
public:
ArrayList();
bool empty() const {return listSize ==0;}
int size() const {return listSize;}
int capacity() const {return arrayLength;}
void insert(int index, Person *p); //insertion sort
void output();
protected:
Person* per;
int arrayLength;
int listSize;
};
#endif
ArrayList.cpp:
#include "ArrayList.h"
#include <iostream>
using namespace std;
ArrayList::ArrayList()
{
arrayLength = 10;
listSize = 0;
}
void ArrayList::insert(int index, Person *p)
{
per[index] = *p;
listSize++;
}
void ArrayList::output()
{
for(int i=0; i<listSize; i++)
{
per[i].print();
}
}