Block type is Vaild
Apr 24, 2013 at 6:10pm UTC
Hello every one I am having a problem with this code I am new to C++ but have some experience with C# and Java.Every time this code runs it runs fine until i press enter to close the console then this debug assert error pops up which from the little bit of knowledge I have makes me think that it is in the destructor. I narrowed it down to the creation of the second student object. When I comment it out the code runs fine. part of the assignment was to override the assignment operator to make a copy of the student object dynamic array and all. This is an assignment from class and I have gone over this with my teacher but he has no experience with c++. He says it is because I am not using typedef to assign the pointer name to the dynamic array I tried to explain to him that is just a way to give a type an alias(am I wrong).If someone could please just let know were I went wrong that would very helpful thank you.(I will post all of it I am not sure if you need to see the header file or not)
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 127 128 129 130 131
#include <iostream>
class Student
{
public :
Student(void );
Student(std::string, int );
~Student(void );
std::string getStudentName();
void setNameOfStudent(std::string);
void setListOfClasses(std::string, int );
std::string getListOfClasses(int );
int getNumberOfClasses();
void setNumberOfClasses(int );
void emptyStudent();
Student operator =(const Student);
private :
std::string _studentName;
int _numberOfClasses;
std::string *_listOfClasses;
};
#include "stdafx.h"
#include "Student.h"
#include <iostream>
Student::Student(void ) { }
Student::Student(std::string name, int num)
{
_studentName = name;
_numberOfClasses = num;
_listOfClasses = new std::string[_numberOfClasses];
}
Student::~Student(void )
{
delete [] _listOfClasses;
}
std::string Student::getStudentName()
{
return _studentName;
}
void Student::setNameOfStudent(std::string name)
{
_studentName = name;
}
void Student::setListOfClasses(std::string className, int index)
{
_listOfClasses[index] = className;
}
std::string Student::getListOfClasses(int index)
{
return _listOfClasses[index];
}
int Student::getNumberOfClasses()
{
return _numberOfClasses;
}
void Student::setNumberOfClasses(int num)
{
_numberOfClasses = num;
}
void Student::emptyStudent()
{
for (int i =0; i < _numberOfClasses; i++)
{
_listOfClasses = NULL; //Makes list of classes a empty string
}
_numberOfClasses =0;
}
Student Student::operator =(const Student studentToCopy )
{
Student newStudent(studentToCopy._studentName, studentToCopy._numberOfClasses);
memcpy ( &newStudent._listOfClasses, &studentToCopy._listOfClasses, sizeof (std::string)); //makes copy of data not pointer
return studentToCopy; // --> I just put a break point here and this is not running
}
#include "stdafx.h"
#include "Student.h"
#include <iostream>
#include <string>
void createAndPopulateStudentObject(Student&);
//creates student object and initializes it
// with input from console
void displayOutputToConsole(Student&);
//displays the output from the student
// object to the console
int _tmain(int argc, _TCHAR* argv[])
{
Student studentOne("Otis" , 5);
createAndPopulateStudentObject(studentOne);
displayOutputToConsole(studentOne);
Student copyOfStudentOne.operator = studentOne;
displayOutputToConsole(copyOfStudentOne);
system("PAUSE" );
return 0;
}
void createAndPopulateStudentObject(Student& studentOne)
{
std::string tempstr;
for (int i =0; i < studentOne.getNumberOfClasses(); i++)
{
std::cout << "Enter the name of your number " << i+1 << " class\n" ;
std::cin >> tempstr;
studentOne.setListOfClasses(tempstr,i);
}
}
void displayOutputToConsole(Student& studentOne)
{
std::cout << studentOne.getStudentName() << " is taking " << studentOne.getNumberOfClasses() << " classes this term.\n" ;
for (int i =0; i < studentOne.getNumberOfClasses(); i++)
{
std::cout << "Class number " << i+1 << " is " << studentOne.getListOfClasses(i) << std::endl;
}
}
Last edited on Apr 24, 2013 at 6:31pm UTC
Apr 24, 2013 at 6:30pm UTC
You are also not using the operator correctly. All you need to do is assign the values that the student has to the student that holds the operator:
1 2 3 4 5 6 7
Student& Student::operator =(const Student& S)
{
_studentName = S.getStudentName();
_numberOfClasses = S.getNumberOfClasses();
_listOfClasses = new std::string[_numberOfClasses];
return *this ;
}
Yes?
1 2
Student studentTwo;
studentTwo = studentOne;
Apr 24, 2013 at 6:32pm UTC
Thank you I will do that.
Apr 24, 2013 at 7:02pm UTC
"pogrady" thank you very much that solved my problem.
Topic archived. No new replies allowed.