C++ \main.cpp | 103 |错误:与'不匹配;操作员<<';(操作数类型为';std::ostream{aka std::basic#ostream<;char>;}';和';Person';)
这是我的第一个问题,所以请容忍我犯的任何格式错误,我将尝试编辑它们:) 我有一个函数,它找到某个数据类型X的三个变量的最大值并返回它C++ \main.cpp | 103 |错误:与'不匹配;操作员<<';(操作数类型为';std::ostream{aka std::basic#ostream<;char>;}';和';Person';),c++,operator-overloading,cout,C++,Operator Overloading,Cout,这是我的第一个问题,所以请容忍我犯的任何格式错误,我将尝试编辑它们:) 我有一个函数,它找到某个数据类型X的三个变量的最大值并返回它 template <class X> X fmax(X a, X b, X c) { X temp=a; if (b>temp) temp=b; if(c>temp) temp=c; return temp; } 我还让ostream超负荷工作: ostream &ope
template <class X>
X fmax(X a, X b, X c)
{
X temp=a;
if (b>temp)
temp=b;
if(c>temp)
temp=c;
return temp;
}
我还让ostream超负荷工作:
ostream &operator<<(ostream &mystream, Person &p)
{
mystream<<"The person's name is: "<<p.getName()<<endl;
mystream<<"The person's height is: "<<p.getHeight()<<endl;
mystream<<"The person's gender is: "<<p.getGender()<<endl;
return mystream;
}
ostream&operator您需要更改以下内容:
由于fmax()
ostream &operator<<(ostream &mystream, const Person &p);
// ^^^^^
int main()
{
Person a("Zacky",178,'m');
Person b("Jimmy",199,'m');
Person c("Matt",200,'m');
Person d=fmax(a,b,c);
cout<<d<<endl;
cout<<fmax(a,b,c);<<endl;//the error strikes here.
return 0;
}
ostream &operator<<(ostream &mystream, const Person &p);
// ^^^^^
class Person
{
// ...
const char* getName() const {return this->name;}
int getHeight() const {return this->height;}
char getGender() const {return this->gender;}
};