1

I have a class:

class cAsset{
  public:
     void data(int);
     int returnInfo(void);
}

and a function which is suppose to return an array of cAssets

cAsset[] myFunc(int a, int b){
   ...
}

The error is:

Expected member name or ';' after declaration specifiers

What am i missing?

Fallenreaper
  • 10,222
  • 12
  • 66
  • 129

1 Answers1

5

You cannot return arrays in C++. Try returning a std::vector<cAsset> instead.

std::vector<cAsset> myFunc(int a, int b){
  std::vector<cAsset> result;
  result.push_back(cAsset(4,2));
  result.push_back(cAsset(a,b));
  return result;
}
Robᵩ
  • 163,533
  • 20
  • 239
  • 308
  • interesting. Is there any reason why? I mean, i thought you can declare arrays, or is it just because a vector accepts a generic class? – Fallenreaper Nov 06 '12 at 20:17
  • 1
    You can declare arrays. You just can't assign to/from them, nor return them from a function. As to "why" : arrays and pointers work the way they did in C, that is, efficiently and non-intuitively. – Robᵩ Nov 06 '12 at 20:19
  • ahh, ok. Good to know. Thanks for the help @Rob. I was wondering why this occurred. – Fallenreaper Nov 06 '12 at 20:20
  • Another option, in C++11 is `std::array`, where N is a compile-time known constant. – Dave S Nov 06 '12 at 21:24