Dear all,
I am now trying to write a void function which would give an array of 360 elements as output for other parts of the program. I wrote something like this.
However, the compiler would not compile the program.
What is wrong? Ideally, I would imagine after doing the ... things the function will output an array of 360 sums and an array of 360 counts for the next part of my program...
The C++ standard explicitly says there shall be no arrays of references, only references to arrays. Why? A reference must be initialised with a valid object. Since there's no guarantee that all references will be initialised, it would break the rules of the standard. Therefore, such a declaration is deemed illegal.
If you meant to declare a reference to an array, then the syntax is a little different:
1 2
int Array[] = {1, 2, 3 };
int(&RefToAnArray)[3](Array);
Note the parentheses enclosing the ampersand. This allows the compiler to distinguish between an array of references from a reference to an array.
Thanks all I guess I will just take Andy's approach. I will just use vector instead of array, initialize the vectors by pushing back 360 zeros into each of the vectors, then use the two vectors as arrays.
Thank you all.
P.S. Vectors seems so much more convenient than arrays. Why would people still use arrays then actually...?
- Efficiency. std::vectors are situated on the heap/free-store, whereas arrays are situated on the stack
- Efficiency, again. std::vector allocates memory for many objects of type T (usually more that 100,000)
Hello guys,
I know this sound a bit stupid, can I define a class type VALUE, with this_value of type VALUE, and that this class type VALUE basically only consists of constructors and that array[] which i want to output from the function, and then I just do...
can I define a class type VALUE, with this_value of type VALUE, and that this class type VALUE basically only consists of constructors and that array[] which i want to output from the function, and then I just do...
Has anyone really been far even as decided to use even go want to do look more like?
Yes, I guess you can do that, but why?
i don't quite understand what you guys mean by call by value, call by reference and this thing below:
int(&RefToAnArray)[3](Array);
That "thing below" is just a reference to an array of 3 int's.
A reference can be thought of as a different name for a variable and not a variable on its own.
If your compiler allocates a 400KB block for a vector<int>(1), file a bug report.
I don't know much about vectors, but the heap seems to work differently than the stack.
On my school's Win7
1 2 3 4 5 6
int *array;
array = newint[100000]; // Memory might be allocated, but it isn't used
for (int i = 0; i < 100000; i++)
array[i] = i; // Now the program uses 400k
Like I said, vectors might be different, and I might be wrong, but this is the behavor we noticed.