1

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();
    }
}
  • Pointers are not arrays! Also, your include guard identifier could be better: http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier – chris Feb 13 '13 at 05:15
  • You never allocated memory. Unless you are doing it for learning purposes take a look `std::vector` class. – Asha Feb 13 '13 at 05:17
  • Why make your own `ArrayList` class when there's a perfectly useful `std::vector`? – nneonneo Feb 13 '13 at 05:19
  • @nneonneo: homework, of course – Ed S. Feb 13 '13 at 05:19

1 Answers1

1

Your pointer is uninitialized, it does not refer to a valid memory location. If you're going to implement your data structure this way, you'll need to initialize it and then check if you need to reallocate when inserting.

ArrayList::ArrayList(size_t capacity)
{
    _capacity = capacity;
    _list_size = 0;
    // initialize your backing store
    _per = new Person[_capacity];
}

You'll also need to properly handle deallocation, assignment, copying, etc.

Ed S.
  • 122,712
  • 22
  • 185
  • 265
  • So then by adding the per = new Person[capacity] line the array would be initialized? Also, what is the type "size_t" in the parameter of the constructor? Sorry, I'm very new at c++ and the idea of pointers to objects is still confusing to me. I have found out how to create an array list of primitive data types but making one using objects is confusing me. –  Feb 13 '13 at 21:40
  • `operator new []` returns a pointer to a block of memory which holds `capacity` number of default initialized objects (in this case, `Person` objects). `size_t` can be found in the documentation: http://en.cppreference.com/w/cpp/types/size_t – Ed S. Feb 13 '13 at 22:18