简体   繁体   中英

\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.

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 &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. 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 :/

You need to change the following things:

Since fmax() returns an unchangable rvalue you should use

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:

class Person
{
   // ...
    const char* getName() const {return this->name;}
    int getHeight() const {return this->height;}
    char getGender() const {return this->gender;}
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM