0

I get this error while trying to create a copy assignment constructor: "overloaded operator must be a binary operator"

this is my code: header:

class User{
public:
    User(const std::string& name);
    const User& operator=(const User &other);
    //virtual Watchable* getRecommendation(Session& s) = 0;
    std::string getName() const;
    std::vector<Watchable*> get_history() const;
protected:
    std::vector<Watchable*> history;
private:
    const std::string name;

}; 

cpp:

User::User(const std::string& name):name(name) {

}

const User& operator=(const User &other){

}

the error is in the cpp file. anyone?

Maya Saias
  • 37
  • 9
  • 2
    sorta typo: `const User& operator=(const User &other){}` should be `const User& User::operator=(const User &other){}` – user4581301 Nov 20 '19 at 23:02
  • 1
    In addition to what @user has said, you're promising to return a `const User&`, but you return nothing. Since you got it right on the constructor, it's clear you know you should need the `User::`. I'm voting to close this as a typo. – scohe001 Nov 20 '19 at 23:02
  • 1
    Sidenote: Per the [Rule of Three](https://en.cppreference.com/w/cpp/language/rule_of_three), if you need an assignment operator, you probably need a Destructor and a Copy Constructor. – user4581301 Nov 20 '19 at 23:03
  • And if you have a copy constructor, you can take advantage of the quick and surprisingly clean [Copy and Swap Idiom](https://stackoverflow.com/questions/3279543/what-is-the-copy-and-swap-idiom) to write the assignment operator. – user4581301 Nov 20 '19 at 23:05
  • Thank you everyone, the reason I did not add the User:: is because I read somewhere it should be non member function and therefore should have it, but I am new to c++. yes I will add the Destructor and Copy Constructor as well. again thank you – Maya Saias Nov 20 '19 at 23:15
  • Many operators should be free functions, but the assignment operator is inherently tied to the object that is being assigned. As a result it can only be implemented as a member function. For more useful tidbits on operators see [What are the basic rules and idioms for operator overloading?](https://stackoverflow.com/questions/4421706/what-are-the-basic-rules-and-idioms-for-operator-overloading). – user4581301 Nov 20 '19 at 23:27
  • Ok, so I made the change you said and the error was gone, but now when I do: User* new_user = original_user; it does not go to the operator assignment. what am I doing wrong? – Maya Saias Nov 20 '19 at 23:33
  • That is a new question, so open a new question and you can provide your code with another focus. – Tobias Wollgam Nov 21 '19 at 00:00
  • Two things to note with `User* new_user = original_user;` 1) `User* new_user` defines a pointer to a `User`, NOT a `User`. If you assign pointers, you just change an address and point at a different `User`. 2) When you define a variable and instantly set its value you initialize rather than assign. `User new_user = original_user;` would call the copy constructor and construct a new `User` based on `original_user` rather than constructing an empty `User` and then assigning. If you need more than those two hints definitely do ask a new question. Comments aren't able to handle more than hints. – user4581301 Nov 21 '19 at 00:58
  • Ok thank you everyone for your help! – Maya Saias Nov 21 '19 at 09:00

0 Answers0