1

This is a member function:

Circle returnlargetcircle(Circle obj[], int size)
{
    int radi[size];

    for (int i = 0; i < size; i++)
    {
        radi[i] = obj[i].getradius();
    }

    for (int i = 0; i < size; i++)
    {
        if (radi[0] < radi[i])
        {
            radi[0] = radi[i];
        }
    }
}

expression must have a constant value --the value of parameter "size "(declared in line 61) can not be used as constant

What should be done in this case. I can't do this since my compiler is not allowing me to do this. What is an alternate way for this?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
daud nasir
  • 41
  • 4
  • 2
    Using `*` to emphasise something in C++ code is a _really_ bad idea. Edit the question to show the code as it really looks and add `// comments` if needed. – Ted Lyngmo Aug 01 '21 at 11:52
  • 1
    array sizes must be compile time constants. Use a `std::vector` for dynamically sized arrays – 463035818_is_not_an_ai Aug 01 '21 at 11:55
  • If the whole point of this is to get the maximum radius you really don't need `radi` at *all*, and `obj` should be const (as should be `getradius` if the former change ends up breaking your build). – WhozCraig Aug 01 '21 at 12:01
  • A variable length array (with size is determined at run time) is not valid C++. Use `std::vector` (which is a templated class, that manages a dynamically allocated array) instead of an array of `int`. It doesn't behave exactly like an array, and supports operations that an array does not, so read the documentation for `std::vector` before attempting to use it. – Peter Aug 01 '21 at 12:11
  • Does this answer your question? [How do I use arrays in C++?](https://stackoverflow.com/questions/4810664/how-do-i-use-arrays-in-c) – Sreeraj Chundayil Aug 01 '21 at 12:15

1 Answers1

1

Array sizes must be compile time constants. You can use std::vector for dynamically sized arrays.

However, you don't need an array in the first place. Use std::max_element with a custom comparator, and don't forget to return a circle:

Circle returnlargetcircle(Circle obj[], int size) {
    return *std::max_element(obj,obj+size,
                            [](const Circle& a, const Circle& b) {
                                return a.getradius() < b.getreadius();
                            });
}

You should also handle the case when obj is empty. You cannot return a Circle when there is none.

And if this is for an exercise and you are not allowed to use any std:: stuff then you still do not need the extra array:

Circle returnlargetcircle(Circle obj[],int size)
{
    int max_radius = obj[0];
    size_t max_index = 0;

    for (size_t i = 1; i < size; i++) {
        if (obj[i].getradius() > max_radius) {
            max_radius = obj[i].getradius(); 
            max_index = i;
        }
    }
    return obj[i];
}

(again this assumes that obj has at least one element)

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
  • 3
    `std::end(obj)` will not work in this case, because `obj` is only a pointer. But `std::begin(obj)+size` will work. – mch Aug 01 '21 at 12:20
  • @mch are you sure there is an overload of `std::begin()` that takes a pointer as input? [I don't see one](https://en.cppreference.com/w/cpp/iterator/begin). In this situation, `Circle obj[]` is really just `Circle* obj`, so I would just use `obj,obj+size` instead, since `obj` is a decayed pointer to an array element, which is a valid iterator on its own. – Remy Lebeau Aug 01 '21 at 16:15