tags:

views:

90

answers:

1

I have this snippet of the code:

header

class A {
private:
    int player;
public:
    A(int initPlayer = 0);
    A(const A&);
    A& operator=(const A&);
    ~A();
    void foo() const;
friend A& operator=(A& i, const A& member);
};

operator=

A& operator=(A& i, const A& member){   
    i(member.player);
    return i;
}

and I have row in my code:

 i = *pa1;

A *pa1 = new A(a2); at the beginning i was int

how can I fix it, thanks in advance I have an error must be non-static function

+4  A: 

The assignment operator for a class must be a member function, not a friend.

A& operator=( const A& member){   
    this->player = member.player);
    return *this;
}

If you want to convert an A class object to an integer, provide a named conversion function such as ToInt().

As with all your questions, this could easily have been answered by reading a C++ text book. This is the last of such questions from you I will be answering.

anon
at the beginning 'i' is an integer, I want to convert it to my class, how can I do it?
lego69
as I know it doesn't work both sides, I can write *pa1=i, my question is, can I write i=*pa1?
lego69
@lego69 - Read up on conversion member functions. It is possible to define something like `operator int() const` as a member function and your class gains an automatic conversion to `int`. It's generally not recommended though. Automatic conversions can very quickly render code unreadable.
Omnifarious
@Omnifarious: thanks a lot it's really very helpful
lego69