1

I am a total C++ noob, and I am having some trouble returning an array from my methods. I have a header file with the following method declaration:

virtual double[]
echoDoubleArray(double[] doubleArray, int arraySize) throw (RemoteException);

This gives me the following error:

../common/EchoService.h: At global scope:
../common/EchoService.h:25: error: expected unqualified-id before ‘[’ token

What is the correct way to return an array?

3
  • 3
    Keep in mind that exception specifications are officially deprecated. Commented Oct 29, 2011 at 1:22
  • 2
    Don't use exception specifications, they don't do what most people think. They have been deprecated in C++11. Commented Oct 29, 2011 at 1:23
  • The correct syntax for a function returning an array of doubles would be double foo()[5]. However, neither C nor C++ allow functions to return arrays. Commented Oct 29, 2011 at 5:03

6 Answers 6

7

C++ doesn't play nice with non-local arrays, more likely you should be using an actual container like std::array<double,10> or std::vector<double>. I don't think it's possible to return an array from a function.

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

Comments

2

You'll better use std::vector or if you really want to return a plain array, return a pointer (to such an array) dynamically allocated with e.g.

double* arr = new double[arrSize];
// fill arr appropriately
return arr;

But I'll recommend returning a std::vector<double> instead.

Comments

1

Sorry everyone seems to be telling you to use modern STL type programming instead of answering your question. They are right. But you should still understand how things work. The problem with returning arrays are that arrays created in your function will be on the stack. When you leave your function they will not exist anymore. What you can do is return a pointer to an array on the Heap. Research a little more about the stack and the heap to understand. But below is a simple C++ function returning an array allocated on the Heap.

#include <iostream>

double * MakeArray()
{
   double *mydouble = new double[5]; 
   mydouble[1]=1.1;
   mydouble[1]=2.2;
  return mydouble;
}

int main()
{

  double *d=MakeArray();
  std::cout<<d[1]<<d[2];
  delete[] d; 
}

edit:

Thinking about it some more, another way to do it without STL would be returning a struct with an array in it. Most compilers will return a struct by value (assuming your not doing embedded programming).

Probably the best simple way: Pass your array into the function. It is like passing a reference because you are really just passing a pointer.

#include <iostream>

void MakeArray(double d[5])
{
  d[0] = 0.0; d[1] = 1.1;
  d[2] = 2.2; d[3] = 3.3; d[4] =4.4;
}

int main()
{
  double dub[5];
  MakeArray(dub);
  std::cout<<dub[1]<<dub[2];
}

After understanding how everything works. go use the STL.

6 Comments

The problem is not really that arrays are on the stack. So is everything else that is returned, including the pointer to the dynamic array in your function. The problem is more accurately stated by Mooing Duck "C++ doesn't play nice with non-local arrays", because pretty much every other construct can be returned by value, and a copy is made. But arrays cannot.
Note that the 5 in the parameter declaration is ignored. The following are all equivalent: void MakeArray(double d[5]), void MakeArray(double d[]), and void MakeArray(double *d).
@benjamin Lindely : What is the reason it doesnt play nice?
@JoeMcGrath: Like I said, they cannot be returned by value, even though pretty much every other construct can(as long as it has a public copy ctor). Or are you asking me why they can't be returned by value?
@JoeMcGrath: I don't know, it's a history thing that doesn't really interest me much.
|
1

There is a technique for passing arrays that do not decay to pointers. You pass the array by reference, using the syntax below. The required "extra" parens are a syntactical wart, and probably why this usage is not more common. One gets used to it real fast, given the advantages.

void sub(double (&foo)[4])
{
    cout << sizeof(foo) << endl;
}

Prints 32, not sizeof(double*), because the type of foo really is a reference to an array of 4 doubles. Using templates, you can generalize to work for any size of array:

template <int N> sub(double (&foo)[N])
 {
    cout << sizeof(foo) << endl;
 }

 double bar[5];
 double zot[3];
 sub(bar); // ==> 40
 sub(zot); // ==> 24

You can generalize again to handle an any-size array of anything:

template <class T, int N>void sub(T(&foo)[N])
    {
        cout << sizeof(foo) << endl;
    }

double bar[5];
char zot[3];
sub(bar); // ==> 40
sub(zot); // ==> 3

and you now have something that captures the abstract idea of "sub" regardless of the type or size of the array you pass it.

Comments

0

The C++ syntax for arrays is: std::vector<double> instead of double[]. It also requires you to put #include <vector> near the top of your source file. Other than that they work very similar to C-arrays.

3 Comments

this answer is very misleading.
To explain how it's misleading, std::vector is an alternative to arrays, not a replacement for them. C++ still has C-style arrays in all their confusing pointer-dependent glory.
I downvoted because the C++ syntax for arrays is not std::vector.
0

To answer your question more directly, if you declare an array of doubles as follows:

double doubleArray[] = {1.0, 2.0, 3.0};

you would declare the method like this:

virtual double *
echoDoubleArray(double* doubleArray, int arraySize) throw (RemoteException);

Arrays are passed as pointers. If you want to use the true power of C++, then follow the advice provided in the other answers.

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.