jeudi 8 octobre 2020

error: no match for 'operator<'} and 'HeroPower' [duplicate]

I am currently working on this:

In League of Legends there are N heros, the number i hero has a physical damage and magicl damage.

the ith heros is said to be stronger than the jth heros if it has stronger physical damage.

If two hero have the same physical damage, a stronger hero will have more magical damage.

Please indicate the physical damage and magical stats of the m strongest hero.

So I write a class named HeroPower, evaluate the power to be a double type: pysicalDamage.magicalDamage

I am working to sorting this class, ask google and found this site: How to sort a class array

So i tried it on my code and it return this: error: no match for 'operator<<' (operand types are 'std::ostream' {aka 'std::basic_ostream<char>'} and 'HeroPower' "HeroPower is the name of my class"

Why?

My code:

#include <iostream>
#include <cmath>
#include <algorithm>


using namespace std;



class HeroPower
{
    int physicalDamage {};
    int magicalDamage {};

public:

    void getNum(int input1, int input2)
    {
        physicalDamage = input1;
        magicalDamage = input2;
    }

    double actualStrength()
    {
        int temp = magicalDamage;
        int countNum = 0;
        while (temp != 0)
        {
            temp = temp / 10;
            ++countNum;
        }
        return physicalDamage + magicalDamage / pow(10, countNum);
    }
};

class ThingComparator
{
    bool operator()(HeroPower& a,HeroPower& b)
    {
        return a.actualStrength() < b.actualStrength();
    }
};

int main()
{
    int n, m;
    cin >> n >> m;

    HeroPower arrayNum[n];

    for (int i = 0; i < n; i++)
    {
        int input1, input2{};
        cin >> input1 >> input2;
        HeroPower hero {};
        hero.getNum(input1, input2);
        arrayNum[i] = hero;
    }
    sort(arrayNum, arrayNum + n,ThingComparator());
    cout << arrayNum[m - 1];
}

Aucun commentaire:

Enregistrer un commentaire