Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does in-class member initialization delete the assignment operator?

So I am doing a project that has the Student, Address and Transcript class.

The student constructor is the 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 that says: 
   "Transcript::operator=(const Transcript&)" (declared implicitly) cannot be referenced 
   -- it is a deleted function.
}

So I thought that something was wrong with my Transcript constructor. But the instructions were to leave the Transcript default constructor empty except for initializing the variable courseCount to 0. (The program reads student data in from a file and adds courses to a students transcript).

I've been stuck for a while can anyone help on how to fix this deleted function error?

Here is the transcript constructor if you 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;
};
like image 898
C. Sullivan Avatar asked Jun 26 '26 22:06

C. Sullivan


1 Answers

This has nothing to do with Transcript's constructor. At the point the error gets reported the Transcript class member is not being constructed. It's already been constructed.

class A {

public:

     A() {}

   // ...

};

class B {

   int c;
   A a;

public:

   B() : c(0) // a gets constructed at this point
   {
       a=A();
   }
};

This is an example analogous to your situation. When an instance of B gets constructed, all of B's class members, including a, get constructed by B's constructor's initialization list, at the point indicated by the comment.

By the time "a=A();" happens, a has already been constructed, and a=A(); invokes A's assignment operator.

Because your Transcript class has a const class member, it cannot have a default assignment operator. A default assignment operator essentially assigns each class member, one by one, but because one of your class members is const, it can't be assigned, so you should implement your own assignment operator.

However, it's obvious that you meant to declare a static const class member, in this case:

static const int MAX_STUDENT_COUNT = 50;

This is likely the reason why your class's assignment operator (NOT a constructor) has been deleted, and you are getting this error.

like image 51
Sam Varshavchik Avatar answered Jun 29 '26 12:06

Sam Varshavchik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!