I have a class named Student
class Student
{ string name;
unsigned long int ID ;
string email;
unsigned short int year;
public :
Student() // Constructor
string getName(void);
unsigned long int getID(void);
string getEmail(void);
unsigned short int getYear(void);
{
and another class named eClass
class eClass {
private:
string eclass_name;
Student* students[100];
unsigned int student_count;
public:
eClass(string name)
{
student_count =0 ;
eclass_name = name ;
}
bool exists(Student obj)
{
unsigned long int code = obj.getID();
bool flag = TRUE ;
for (unsigned int i = 0 ; i<=student_count ; i++ )
{
unsigned long int st = (*students[i]).getID();
if (code==st)
{
flag = FALSE;
}
}
return flag;
}
void add(Student& obj)
{
bool res = exists(obj);
if (res)
{
students[student_count] = new Student(); //probably the problem is here
*students[student_count] = obj ;
student_count++ ;
}
}
string getEclassName(void) { return eclass_name; }
unsigned int getStudentCount(void) { return student_count; }
Student getStudent(int i) { return *students[i-1]; }
};
The statement Student* students[100]; must look exactly like this . For example I can't write something like this: Student* students[100]={} ;
And main() looks like this
int main()
{
Student JohnDoe("John Doe", 12345, 2, "[email protected]");
eClass Cpp("C++");
Cpp.add(JohnDoe);
}
Basically I have an array of pointers to Student objects and I want to allocate dynamically memory every time I want to add a new Student object.
When I compile I get no errors but when I try to run the program the only thing I get is "Program_name.exe" stopped running...
I'm pretty sure the problem has to do with memory allocation but I'm not able to find it and solve it.
Any suggestions ?
Student?Students? Just storeStudents.exists(Student obj). Anyway, you shouldn't be passing by value into that function (but I don't think that is the major bug). Add your implementation ofexiststo you post.exists. (Side note:existsreturningtrueif the student doesn't exist is very counter-intuitive.)