-5

I am writing c++ integer array class myself and a function for extracting sub-arrays but I am getting this error while trying to compile my code:

"Multiple markers at this line - No return, in function returning non-void - with ‘Array Array::subArr(int, int)’"

(sorry forgot to include this error)

- ‘int* Array::subArr(int, int)’ cannot be overloaded

But why cant it be overloaded?

The private data members of class Array are:

private:
    int *arr;
    int *subArray;
    int *subArr1;
    int len;

The two public functions causing the error are:

int * subArr(int pos, int siz)
{
    if (pos+siz < len)
    {
        subArr1 = new int[siz];
        for (int i=0; i<siz; i++)
        {
            subArr1[i] = arr[pos];
            pos++;
        }
        return subArr1;
    }
}

and

Array subArr(int pos, int siz)
{
    if (arr != NULL && (siz + pos) < len)
    {
        Array sub;
        sub.len = siz;
        sub.subArray = new int [siz];
        for (int i=0; i<siz; i++)
        {
            sub.subArray[i] = arr[pos];
            pos++;
        }
        return sub;
    }
    cout<<"Error. Enter a valid index and/or position\n";
}

I can send the whole class code if it is required.

Abdul Mateen
  • 1,418
  • 1
  • 14
  • 32

2 Answers2

5

It's just what it says;

No return, in function returning non-void - with ‘Array Array::subArr(int, int)’"

Look at the code:

int * subArr(int pos, int siz)
{
    if (pos+siz < len)
    {
        subArr1 = new int[siz];
        for (int i=0; i<siz; i++)
        {
            subArr1[i] = arr[pos];
            pos++;
        }
        return subArr1;
    }
}

What happens if the if (pos+siz < len) is false?

Correct, you don't have anything to return, in a function declared to return int *. That's an error.


‘int* Array::subArr(int, int)’ cannot be overloaded

Look at the code:

int * subArr(int pos, int siz)
Array subArr(int pos, int siz)

The two functions differ only in return type. That is not possible; there must be a difference in the number and / or the type of the parameters.


Gratuitous generic advice:

  • Avoid reimplementing standard functionality, other than as exercise.
  • Avoid using pointers.
  • Avoid new unless...

    • in a constructor where your destructor has the corresponding delete.
    • wrapped in a smart pointer allocation.

    (Allocating memory inside a function and then expecting the caller to free it again is a prime source for memory leaks.)

  • Don't write errors to cout, throw an exception.
  • Use nullptr, not NULL.

In short, write C++, not C with classes -- that was thirty years ago.

DevSolar
  • 67,862
  • 21
  • 134
  • 209
1

It is possible to reach the end of your functions and you do not return anything at the end. If you have a function that is suposed to return something then every path out of the function needs to have a return. Basically

int * subArr(int pos, int siz)
{
    if (pos+siz < len)
    {
        subArr1 = new int[siz];
        for (int i=0; i<siz; i++)
        {
            subArr1[i] = arr[pos];
            pos++;
        }
        return subArr1;
    }
}

Becomes

int * subArr(int pos, int siz)
{

}

if if (pos+siz < len) is false. Since there is no return you get the error.


You second issue is you cannot overload a function based on its return type alone.

Array subArr(int pos, int siz)
int * subArr(int pos, int siz)

Ignoring the return type on the function we have

subArr(int pos, int siz)
subArr(int pos, int siz)

How is it supposed to tell them apart? It can't. That is why it is not allowed.

NathanOliver
  • 171,901
  • 28
  • 288
  • 402