Hallo an alle, ich versuche ein pair
wie Vorlage zu implementieren. Ich habe es versucht:Überladungszuweisung (=) Operator in Paar Vorlage
#include<iostream>
using namespace std;
template<class T1, class T2>
class Pair
{
//defining two points
public:
T1 first;
T2 second;
//default constructor
Pair():first(T1()), second(T2())
{}
//parametrized constructor
Pair(T1 f, T2 s) : first(f),second(s)
{}
//copy constructor
Pair(const Pair<T1,T2>& otherPair) : first(otherPair.first), second(otherPair.second)
{}
//overloading == operator
bool operator == (const Pair<T1, T2>& otherPair) const
{
return (first == otherPair.first) && (second == otherPair.second);
}
//overloading = operator
Pair<T1, T2> operator = (const Pair<T1, T2>& otherPair)
{
first=otherPair.first;
second=otherPair.second;
return this;
}
int main()
{
Pair<int, int> p1(10,20);
Pair<int, int> p2;
p2 = p1;
}
Aber ich bekomme den Fehler in der letzten Zeile der überladenen Methode =. Es ist nicht erlaubt, das this
Objekt zurückzugeben.
Kann jemand helfen, wo ich falsch mache?
Was ist 'T1' /' T2'? Sie sind nicht deklariert. –
Sie müssen '* This' zurückgeben –
s /' Paar operator = (const Paar & otherPair) '/' Paar & operator = (const Paar & otherPair) 'und s /' zurückgeben; '/' zurück * das; '. –
user0042