简体   繁体   English

\\ main.cpp | 103 |错误:“ operator &lt;&lt;”不匹配(操作数类型为“ std :: ostream {aka std :: basic_ostream <char> }”和“人”)

[英]\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.

相关问题 错误:“operator&lt;&lt;”不匹配(操作数类型为“std::ostream {aka std::basic_ostream”<char> }&#39; 和 &#39;void&#39;) - error: no match for ‘operator<<’ (operand types are ‘std::ostream {aka std::basic_ostream<char>}’ and ‘void’) 错误:'operator&lt;&lt;' 不匹配(操作数类型是 'std::ostream' {aka 'std::basic_ostream<char> '}</char> - error: no match for ‘operator<<’ (operand types are ‘std::ostream’ {aka ‘std::basic_ostream<char>’} 错误:'operator&lt;&lt;' 不匹配(操作数类型是 'std::ostream {aka std::basic_ostream<char> }'和'列表')</char> - error: no match for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream<char>}' and 'List') 继续收到此错误 main.cpp:9:91: error: no match for 'operator&lt;&lt;' (操作数类型是 'std::basic_ostream' 和 'const std::vector' - Keep receiving this error main.cpp:9:91: error: no match for ‘operator<<’ (operand types are ‘std::basic_ostream’ and ‘const std::vector’ 'operator&lt;&lt;' 不匹配(操作数类型是 'std::ostream {aka std::basic_ostream<char> }' 和 '分数')</char> - No match for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream<char>}' and 'Fraction') 与&#39;operator &lt;&lt;&#39;不匹配(操作数类型为&#39;std :: ostream {aka std :: basic_ostream <char> }” - no match for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream<char>}' 错误:'operator&lt;&lt;' 不匹配(操作数类型是 'std::ostream' {aka 'std::basic_ostream<char> '} 和 'std::_List_iterator<int> ')</int></char> - error: no match for ‘operator<<’ (operand types are ‘std::ostream’ {aka ‘std::basic_ostream<char>’} and ‘std::_List_iterator<int>’) 错误:“operator&lt;&lt;”不匹配(操作数类型为“std::basic_ostream”<char> &#39; - error: no match for ‘operator<<’ (operand types are ‘std::basic_ostream<char>’ 如何修复错误:'operator&lt;&lt;' 不匹配(操作数类型为 'std::ostream {aka std::basic_ostream<char> }' 和 'void')同时使用字符串和堆栈</char> - How to fix error: no match for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream<char>}' and 'void') while using strings and stack 'operator&lt;&lt;' 不匹配(操作数类型为 'std::ostream' {aka 'std::basic_ostream<char> '} 和 'const std::type_index')</char> - No match for ‘operator<<’ (operand types are ‘std::ostream’ {aka ‘std::basic_ostream<char>’} and ‘const std::type_index’)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM