0

I have a function called FindMaxByArea that takes in a vector of Rectangle objects and returns one with the biggest length. When I compile my code I keep getting this error:

Rectangle.h:40:23: error: no match for call to ‘(AreaCompare) (const value_type&, const value_type&)’
         if (isLessThan(arr[maxIndex], arr[i]))
             ~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~

I am also not sure if appending {} to the end of AreaCompare is the correct way to call this function. Any suggestions would be appreciated.

return findMax(arr, AreaCompare{});

This is my Rectangle.h file

#pragma once

#ifndef Rectangle_H
#define Rectangle_H

#include <vector>
#include <iostream>
#include <functional>


using namespace std;

/* 
    Rectangle class
*/
class Rectangle
{
public:
    int GetLength() const { return m_Length; }
    int GetWidth() const { return m_Width; }
    int getArea() const { return m_Length * m_Width; }
    int getPerimeter() const { return (m_Width * 2) + (m_Length * 2); }
    Rectangle(int length, int width) : m_Length(length), m_Width(width) {}

private:
    int m_Length;
    int m_Width;
};

/* 
    Function that takes in a vector and a compare function and
    returns a generic with the largest properties
*/
template <typename Object, typename Comparator>
const Object & findMax(const vector<Object> & arr, Comparator isLessThan)
{
    int maxIndex = 0;

    for (int i = 1; i < arr.size(); ++i)
        if (isLessThan(arr[maxIndex], arr[i]))
            maxIndex = i;

    return arr[maxIndex];
}

/*
    A compare function that compares the area of two rectangles
*/
class AreaCompare
{
public:
    bool isLessThan(const Rectangle &lhs, const Rectangle &rhs) const
    {
        return lhs.getArea() < rhs.getArea();
    }
};

/*
    A compare function that compares the perimeter of two rectangles
*/
class PerimeterCompare
{
public:
    bool isLessThan(const Rectangle &lhs, const Rectangle &rhs) const
    {
        return lhs.getPerimeter() < rhs.getPerimeter();
    }
};

/* 
    Function compares a vector of rectangles and returns one with
    the largest area
*/
template <typename Object>
const Object & FindMaxByArea(const vector<Object> & arr)
{
    return findMax(arr, AreaCompare{});
}

/* 
    Function compares a vector of rectangles and returns one with
    the largest perimeter
*/
template <typename Object>
const Object & FindMaxByPerim(const vector<Object> & arr)
{
    return findMax(arr, PerimeterCompare{});
}



#endif

main.cpp

#include <iostream>
#include <vector>
#include "Rectangle.h"

int main() {
vector<Rectangle> vec
    {
        Rectangle(1, 3),
        Rectangle(4, 4)
    };

cout << "Highest Area: " << FindMaxByArea(vec) << endl;

return 0;
}
2
  • How are you calling FindMaxByArea? (What type is the arr parameter you're passing to it?) Commented Apr 25, 2020 at 3:05
  • @1201ProgramAlarm I added main.cpp. arr is vector of Rectangles Commented Apr 25, 2020 at 3:14

1 Answer 1

2

In findMax, Comparator isLessThan is a parameter. It is a variable like any other parameter. To call a member function of that type, you'll want to access it thru the variable:

if (isLessThan.isLessThan(arr[maxIndex], arr[i]))

And, since there are no class members, isLessThan could be a static member of the class.

As an alternative you can define a bool operator()(const Rectangle &lhs, const Rectangle &rhs) member function, then your original code would work.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.