标签:
错误例程:
#include<iostream>
using namespace std;
template<class T>
class Student
{
private:
T age;
public:
Student(T age_) :age(age_){}
friend bool operator==(const Student<T>& s1, const Student<T>& s2);
};
int main()
{
Student<int> s1(1);
Student<int> s2(2);
if (s1 == s2)
cout << "相等" << endl;
else
cout << "不相等" << endl;
cin.get();
return 0;
}
template<class T>
bool operator==(const Student<T>& s1, const Student<T>& s2)
{
if (s2.age == s1.age)
{
return true;
}
return false;
}
正确例程:
#include<iostream>
using namespace std;
template<class T>
class Student
{
private:
T age;
public:
Student(T age_) :age(age_){}
template<class T>
friend bool operator==(const Student<T>& s1, const Student<T>& s2);
};
int main()
{
Student<int> s1(1);
Student<int> s2(2);
if (s1 == s2)
cout << "相等" << endl;
else
cout << "不相等" << endl;
cin.get();
return 0;
}
template<class T>
bool operator==(const Student<T>& s1, const Student<T>& s2)
{
if (s2.age == s1.age)
{
return true;
}
return false;
}标签:
原文地址:http://blog.csdn.net/linukey/article/details/46367725