Tuesday, July 28, 2009

Why am i getting this c++ error?

class Offered_Courses {


public:


Offered_Courses (char input[]) {


in.open(input);


assert(in.is_open());


int i = 0;





while(!in.eof()) {


//getline(in, input_line);


in %26gt;%26gt; cn;


in %26gt;%26gt; c;


in %26gt;%26gt; so;


in %26gt;%26gt; t;


courses[i] = new Course(cn, c, so, t);


i++;


}


}


class Course {


public:


Course(string cn, int c, char so, string t) {


course_name = cn;


credits = c;


semester_offered = so;


tag = t;


}


private:


string course_name;


int credits;


char semester_offered;


string tag;


};





private:


ifstream in;


Course courses[MAX_NUM_COURSES];


string cn;


int c;


char so;


string t;


string input_line;


};








- In my above code i seem to be getting some errors with the Course class inside the Offered_Courses class, can anyone tell me whats wrong with the code?





error: no matching function for call to Offered_Courses::Course::Course()





THANKS!!

Why am i getting this c++ error?
You are going to run into numerous problems but the one you are asking about stems from this:





Course courses[MAX_NUM_COURSES];





When Offered_Courses is constructed it is going to try and allocate an array of Course types before it even enters the Offered_Courses constructor. To do this it will call the default constructor for Course for each element of the array. However you have not defined a default constructor so you are getting the compiler error.
Reply:What type are cn, c, so, t?


No comments:

Post a Comment