[英]\main.cpp|103|error: no match for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream<char>}' and 'Person')
this is my first question so please bear with any formatting mistakes i make, I'll try to edit them :) 这是我的第一个问题,所以请忍受我犯的任何格式错误,我将尝试对其进行编辑:)
I've got this function which finds the max of three variables of some data type X and returns it. 我有这个函数,它找到某个数据类型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;
}
Then there is the class Person which looks like this 然后是班级人,看起来像这样
class Person
{
private:
char* name;
int height;
char gender;
public:
Person(){}
Person(char * name,int height, char gender)
{
int sz=strlen(name);
this->name= new char [sz];
strcpy(this->name,name);
this->height=height;
this->gender=gender;
}
void setName(char* name)
{
int sz=strlen(name);
this->name= new char [sz];
strcpy(this->name,name);
}
void setHeight(int h){this->height=h;}
void setGender(char g){this->gender=g;}
char* getName(){return this->name;}
int getHeight(){return this->height;}
char getGender(){return this->gender;}
Person operator= (Person p)
{
int sz=strlen(p.getName());
this->name= new char [sz];
strcpy(this->name,p.getName());
this->height=p.getHeight();
this->gender=p.getGender();
return *this;
}
bool operator> (Person p)
{
if(this->getHeight()>p.getHeight())//The persons should be compared using their heights.
return true;
return false;
}
};
and I also overloaded the ostream: 而且我也重载了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;
}
But I'm getting the error in my main: 但是我的主要错误:
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;
}
Apparently I can cout the object d after i've initialised it using the fmax function but can't directly cout what is returned by the function. 显然,我可以使用fmax函数初始化对象d后退出它,但不能直接退出该函数返回的内容。 Any idea about what I need to fix? 关于我需要解决的任何想法?
PS I'm totally sorry if this was asked before, I searched the site and didn't find something similar :/ PS我很抱歉,如果以前曾问过这个问题,我搜索了该网站,却没有找到类似的东西:/
You need to change the following things: 您需要更改以下内容:
Since fmax()
returns an unchangable rvalue you should use 由于fmax()
返回不可更改的右值,因此您应该使用
ostream &operator<<(ostream &mystream, const Person &p);
// ^^^^^
as signature for the output operator overloading (I would recommend doing that in general). 作为输出运算符重载的签名(我通常建议这样做)。
That in turn would require to make your getter functions const
member functions: 进而需要使您的getter函数成为const
成员函数:
class Person
{
// ...
const char* getName() const {return this->name;}
int getHeight() const {return this->height;}
char getGender() const {return this->gender;}
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.