Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ globally overloaded operator= [duplicate]

Possible Duplicate:
What does “operator = must be a non-static member” mean? (C++)

Hi,

I have the following code...

// Header file
  struct dataRecord{
     size_t id;
     char name[gcNameLength];
  };

  void operator=(dataRecord &adr, const dataRecord &bdr);

How ever gcc gives me the following error when compiling.

error: ‘void operator=(dataRecord&, const dataRecord&)’ must be a nonstatic member function

Thanks for the help.

like image 247
Thomas Avatar asked Oct 19 '25 07:10

Thomas


2 Answers

You need to overload = operation on the struct dataRecord itself.

Something like:

struct dataRecord{
   size_t id;
   char name[gcNameLength];
   dataRecord& operator= (const dataRecord&) {
       // write overload code here
   }
};
like image 108
Pablo Santa Cruz Avatar answered Oct 21 '25 22:10

Pablo Santa Cruz


There is not such a thing as an operator= function. The operator has to be a member of the class or struct. The argument for that function is taken as the rvalue. The object with the member function is the lvalue.

like image 20
J. Velazquez-Muriel Avatar answered Oct 21 '25 21:10

J. Velazquez-Muriel



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!