Inheritance Seg Fault

I'm writing a program that contains an Athlete class, and a college athlete class. A separate class AthleteList contains a list of college athletes

first the Superclass.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void Athlete::setID(int id) {
    this->athleteID = id;
}

void Athlete::setName(string name) {
    this->athleteName = name;
}

void Athlete::setSchool(string school) {
    this->schoolChoice = school;
}

void Athlete::setLOI(string loi) {
    this->athleteLOI = (loi == "yes");
}


the Class containing the list of College athletes

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

ColAthleteList::AthleteList() {
    this->size = 0;
    this->capacity = 2;
    this->list = new BUAthlete[this->size];


}

void ColAthleteList::addAthlete(int ID, string name, string LOI, string school) {
    this->list[this->size].setID(ID);
    this->list[this->size].setName(name);
    this->list[this->size].setLOI(LOI);
    this->list[this->size].setSchool(school);

    this->size++;
}

The program in its entirety is much longer so I've cut out most of the functions to narrow down the problem. If you need to see the rest of the code or the header files, https://github.com/MarkFuller1/Project3/blob/master/BUAthleteList.hpp

when I call setName from the .addAthlete() function i receive a segfault. I don't know why this happens, the lists are all initialized and all the types match. The .setID() fulction works file.

main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include "AthleteList.hpp"
#include "Athlete.h"
#include "Athlete.hpp"


using namespace std;

int main(){

    AthleteList list;

    list.addAthlete(123, "", "", "");

    return 0;
}




My sincerest thanks for your time :)
Last edited on
this->list = new BUAthlete[this->size];
`size' is 0 at this point, so you are creating an array that holds 0 elements

1
2
3
    this->list[this->size].setSchool(school); //here you are trying to access an invalid position

    this->size++; //this will not resize the array 

use std::vector, or at least study it.
Oh wow, silly mistake, in my efforts to reduce the code so that i could post I had to take out the resizing function.

Thank You so much!
Last edited on
Topic archived. No new replies allowed.