I get a Segmentation fault error and I cant find what's wrong with the program.
programs fails when it tries to execute this line "delete [] this->courseList"
in operator = overloading function.

#include <iostream>
#include <string>

using namespace std;

class Student{
        //A string that stores the name of the student
	string name;	 
	int numCourses;

	//"courseList is a pointer that points to an address of a string array"
	string *courseList;

	Student(string, int, string *);


	//copy constructor
	Student(const Student &);

	Student Student:: operator  =  (const Student &);
	void Student::outputCourses();

Student::Student(string name, int numCourses, string *courseList){
	this->name = name;
	this->numCourses = numCourses;
	//"courseList is a pointer that points to an address of a string array"
	this->courseList = new string[numCourses];
	this->courseList = courseList;

void Student::outputCourses(){
	cout << endl;
	//cout << this->objName <<" object content:\n";
	cout << "course(s) of " << this->name << "\n";
	for(int i=0 ; i<this->numCourses ; i++){
		cout << "course number " << i+1 << " is " << courseList[i] << "\n";


//operator = overloading
Student Student:: operator  =  (const Student &right){
	if(this->numCourses != right.numCourses)
		cout << "courseList address:" << &courseList <<endl;
		delete [] this->courseList;
		this->courseList = new string[right.numCourses];
	this->name = right.name;
	this->numCourses = right.numCourses;

	for (int i=0;i<right.numCourses;i++)
		this->courseList[i]= right.courseList[i];//copy each array element
	return *this;

//copy constructor
Student::Student(const Student &obj){
	this->name = obj.name;
	this->numCourses = obj.numCourses;
	courseList = new string[obj.numCourses];

	for (int i=0;i<obj.numCourses;i++)
		courseList[i]= obj.courseList[i];//copy each array element

cout << "destructor running....\n";
cout << "courseList address: " << &courseList << " - deleteing courseList varible\n";
delete [] courseList;  //courseList points to a dynamically allocated array so [] needed to free the memory

int main(){
	cout << "----------------test 2-----------------\n";
	//invokes both copy constructor and assignment operator

	string johnCourses[5]  = { "Math", "Biology", "Science", "History", "English" };
	Student j("John", 5, johnCourses);

	string peterCourses[2]  = { "cPlusPlus", "Calculus"};
	Student p("peter", 2, peterCourses);

	p = j;

	cout << "-----------\n";

        return 0;
6 Years
Discussion Span
Last Post by arkoenig

Lines 35 and 36 are wrong.

I think you think that they allocate an array with numCourses elements and copy the contents of the array to which courseList points into that array. They don't; what they do is put in this->courseList a pointer to a newly allocated array, then throw that pointer away and make this->courseList point to the same array as the one to which courseList points.

I am guessing you really wanted to do something like this:

this->courseList = new string[numCourses];
std::copy(this->courseList, this->courseList+numCourses, numCourses);

the second line of which, by the way, is an easier means of accomplishing what you did in lines 65-68.

Also, line 56 doesn't do anything, so it's probably not what you intended.

There may well be other errors in the code, but I don't see much point in looking for them until you fix these and try it again.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.