-5

I have this in my code:

const Member & Member::operator=( const Member & rhs )
{
    strcpy(  this->firstName, rhs.firstName ); 
    strcpy( this->lastName ,  rhs.lastName );
    this->gender = rhs.gender;
    this->year = rhs.year;
    return *this;
}

and for some reason it doesn't work if I use it.

It tells me that can't convert Member to Member& my teacher has it in a different class the same way and his works. I don't get why I think the signature is right and so is the return value. Some explanation could help. Thank you

Member found;

found = vertices[0].find(set.familyHead); //returns a member

ERROR:

C:\Users\Kyle\Desktop\p4-2>make
g++ -ansi -Wno-reorder -Wall -g -c familyRunner.cpp
g++ -ansi -Wno-reorder -Wall -g -c familytree.cpp
familytree.cpp: In member function 'void FamilyTree::findSet(FamilySet&)':
familytree.cpp:26:8: error: no match for 'operator=' (operand types are 'Member'
 and 'Member')
  found = vertices[0].find(set.familyHead);
        ^
familytree.cpp:26:8: note: candidate is:
In file included from familytree.h:6:0,
                 from familytree.cpp:1:
Member.h:19:17: note: const Member& Member::operator=(Member&)
  const Member & operator=(  Member & rhs );
                 ^
Member.h:19:17: note:   no known conversion for argument 1 from 'Member' to 'Member&'
Makefile:8: recipe for target 'familytree.o' failed
make: *** [familytree.o] Error 1

teacher's signature:

const string & operator= ( const string & rhs );  // Copy
Kyle Calica-St
  • 2,629
  • 4
  • 26
  • 56
  • 1
    Show us code where you perform assignment of Member objects. – myaut Feb 23 '15 at 21:47
  • Also, `operator=` is usually defined as taking a `const` reference, since it should never modify `rhs`. – aruisdante Feb 23 '15 at 21:48
  • 2
    Just FYI, you don't need `this->` in C++, except under certain circumstances when templates are involved. (But it is sometimes good to write `this->` for clarity, anyway.) – Brian Bi Feb 23 '15 at 21:48
  • Show the `class` declaration. BTW, you should use `std::string` – Basile Starynkevitch Feb 23 '15 at 21:49
  • 1
    Almost certainly, you *don't* have it the same way your teacher has it. –  Feb 23 '15 at 21:50
  • 1
    You're learning about assignment operators, but not learning to use `std::string`? – PaulMcKenzie Feb 23 '15 at 21:50
  • am I mixing up the difference between copy and assignment? Could someone please explain or show the differences between the two's signatures – Kyle Calica-St Feb 23 '15 at 21:53
  • can't use string. I'll post an example and my teacher's code – Kyle Calica-St Feb 23 '15 at 21:54
  • The usual signature would be `Member& Member::operator=(const Member & rhs )`. Still, there is not enough information to figure out what is wrong. – juanchopanza Feb 23 '15 at 21:54
  • `can't use string.` Man, this `std::string` has some sort of contagious disease or something. Why so many say "can't use std::string?" And in your case, especially now that you're learning what copy and assignment are, it requires that you make your copies with no chance of error. – PaulMcKenzie Feb 23 '15 at 21:57
  • its cause he built his own string class we use. But here he told us that strcpy would be better to work with. also I forgot const when I was just playing with the code – Kyle Calica-St Feb 23 '15 at 22:00
  • @csCarngie So why don't you use this string class? `But here he told us that strcpy would be better to work with`???? Your teacher has no confidence in their own coding?? If the string class they coded is safe, then use it. It is much better than calling raw `strcpy`, where you have no control if a string buffer does not have enough room. – PaulMcKenzie Feb 23 '15 at 22:02
  • I don't know. I'm just following instructions I already asked if I could use it. He prolly doesn't. But I do need to know why I can't assign something to member. – Kyle Calica-St Feb 23 '15 at 22:04
  • 1
    @csCarngie You are returning a const object. How is `found` declared? As to your teacher, I would crash the app on purpose and show them why *not* using their string class causes problems. – PaulMcKenzie Feb 23 '15 at 22:05
  • Yeah I am not a fan of the school or how CS is generally taught either. I think this assignment doesn't really teach much concept more than annoyance. Cause I’m just showing you a small part of what the whole program is. – Kyle Calica-St Feb 24 '15 at 04:23
  • I did some research and forgot about the big 3 and read up on that and got it done. So thanks! – Kyle Calica-St Feb 24 '15 at 04:24

1 Answers1

3

More than likely you're trying to use an rvalue, something like the following:

A a;
a = A();

If this is the case, the temporary cannot bind to a non-const reference. Hence, the error. You can use the more conventional signature for a copy assignment operator where the parameter is const A&. Another thing you might want to do is changed your return type to A&, to allow operator chaining. For example:

A a;
A b;
A c;
a = b = c;

Finally, I recommend you check out our homebrew FAQ question, What is the copy-and-swap idiom?


The FAQ goes into detail, but currently your copy assignment operator does not account for two major problems:

  • Self-assignment
  • And if the source string is larger than the destination string

Ideally, you should just follow the Rule of zero and use a std::string data member. Your class will automagically do the right thing if you don't labor over manually implementing special member functions.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
  • Thanks, yeah i did some research on the Big 3 and forgot that if you declare one you have to declare them all. Thanks this was a big help. – Kyle Calica-St Feb 24 '15 at 04:25