c++ - Does in-class member initialization delete the assignment operator? -
so doing project has student, address , transcript class.
the student constructor following:
student::student(int eid, string first, string last, address campusaddress, transcript transcript) { seteid(eid); setfirst(first); setlast(last); setaddress(campusaddress); this->transcript = transcript; //error says: "transcript::operator=(const transcript&)" (declared implicitly) cannot referenced -- deleted function. }
so thought wrong transcript constructor. instructions leave transcript default constructor empty except initializing variable coursecount 0. (the program reads student data in file , adds courses students transcript).
i've been stuck while can on how fix deleted function error?
here transcript constructor if need it:
transcript::transcript() { coursecount = 0; }
edit: transcript prototype
class transcript { public: //constructor transcript(); double computegpa() const; void addcourse(const string& course, const int hours, const string& grade); string getcourse(int index) const; string getgradeearned(const int index) const; int getcoursecredithours(const int index) const; int count() const; string tostring() const; private: const int max_student_count = 50; string coursetaken[50]; int coursecredithours[50]; string gradeearned[50]; int coursecount = 0; int computetotalcredithours() const; int computetotalqualitypoints() const; };
this has nothing transcript
's constructor. @ point error gets reported transcript
class member not being constructed. it's been constructed.
class { public: a() {} // ... }; class b { int c; a; public: b() : c(0) // gets constructed @ point { a=a(); } };
this example analogous situation. when instance of b
gets constructed, of b
's class members, including a
, constructed b
's constructor's initialization list, @ point indicated comment.
by time "a=a();" happens, a
has been constructed, , a=a();
invokes a
's assignment operator.
because transcript
class has const
class member, cannot have default assignment operator. default assignment operator assigns each class member, 1 one, because 1 of class members const
, can't assigned, should implement own assignment operator.
however, it's obvious meant declare static const class member, in case:
static const int max_student_count = 50;
this reason why class's assignment operator (not constructor) has been deleted, , getting error.
Comments
Post a Comment