Overloading operator should take exactly one/two arguments!

Edited a little

HI!
I am writing a class Person and it has only one member : string Name.
I want to overload operator >> so that
1
2
3
Person tmp1 ;
Person tmp2 ;
tmp1 >> tmp2 ;

Means to exchange their names with each other.
This is the specified part in Person.h :
void operator >> (Person) ;

And this is the specified part in Person.cpp :
1
2
3
4
5
6
7
void operator >> (Person input)
{
    string tmp ;
    tmp = Name ;
    Name = input.Name ;
    input.Name = tmp ;
}


BUT there is an error in the Person.cpp :
error: 'void operator>>(Person)' must take exactly two arguments



AFTER I SAW THIS I tried changing them into it :
Person.h :
void operator >> (Person , Person) ; Person.cpp :
1
2
3
4
5
6
7
void operator >> (Person input , Person FakeInput)
{
    string tmp ;
    tmp = Name ;
    Name = input.Name ;
    input.Name = tmp ;
}


This time there was an error on Person.h :
error: 'void Person::operator>>(Person, Person)' must take exactly one argument|


I don't know what should I do and where am I wrong!
If I continue using CodeBlocks , one day I will kill myself :)) ;
Last edited on
If I continue using CodeBlocks , one day I will kill myself
Use notepad to type, compile from command line. Error messages will be the same though.

That is because you declared operator>> as a member function in your class and defining it as a freestanding one.
Either move declaration out of class and make it accept two arguments or declare it within class and define it as a class function (same way as you defining constructors and other member functions)

I must say that overloading operators without saving their semantics is a bad design. In your case it would better be a function swapNames(tmp1, tmp2) or tmp1.swapName(tmp2)
closed account (DEUX92yv)
As far as an IDE, I recommend at least TRYING Visual Studio (or Visual Express or another one of its releases).

Concerning your overload: << and >> are (depending on context) bitwise shift, or stream operators. It may be misleading to overload them to do something else.

I see no reason to overload an operator here - why not just define a switchName method or something?
Last edited on
THANK YOU!
I'm just learning classes and how they work.
I have no reason for writing that. I'm just stuck in overloading operaotors.
I have no problem if I want to write that with a function.
I think the problem is with the example I'm trying to write.
I should write something else.

And @MiiNiPaa sorry for the thing I said about CodeBlocks.
I think you like it a little.
I think you like it a little.
Actually I was pointing out that IDE have no relation to the compiler and language standards.

Concerning your overload: << and >> are stream operators. It may be a little misleading to overload them to do something not related to a stream. Having not tried it, I'm not sure if it's even possible.
a) They are bitwise shift operators which were used for some obscure reason to be an stream manipulation operators.
b) It is possible to give them any meaning you want.
c) As you said it is misleading for anyone who will be reading your code. Java forbids operator overloading because of that.
Topic archived. No new replies allowed.