Hello there, I am currently writing a small program and cannot compile it. Each time I compile, I end up with a single error in one file and its corresponding header file. For simplicity, I will just include the header file unless requested:
- #include <cstdlib>
-
-
#ifndef DKS_H
-
#define DKS_H
-
-
using namespace std;
-
-
struct item{
-
int number;
-
float weight;
-
float profit;
-
float pPERw;
-
};
-
-
item[][][] createProfitMatrix(item itemList[], int listSize, int sacWt);
-
#endif
-
Now when I compile the files using GNU C++, I get the error:
error: expected unqualified-id before â[â token
on line 15.
I saw that someone had a similar problem here: http://www.thescripts.com/forum/thread141269.html but I could not gather what was wrong with my code from the answers provided.
Thanks for the help.
The problem is with the return value.
First, an array must define every dim size with the exception of the first one, that you can leave as empty and it will either be determined by the initialiser, or just treated as a pointer depending on the context.
Second, the return format for an array is not that way. Array syntax is pretty messed in C/C++. What you are probably wanting is this:
- item (createProfitMatrix)(item itemList[], int listSize, int sacWt)[Xsize][Ysize][Zsize];
Which means (if I've done it correctly, and I don't pretend to be sure) that the function will return a 3D array of items, which can be captured by using a item pointer to a 2D array of size [Ysize][Zsize]. NOTE: do not return an auto array (an array that is allocated on the function stack). You have been warned.
Third, it looks like you are writing this in C++, so return a vector of a vector of a vector instead or pointer to such. Working with arrays is not for the faint of heart.
Fourth, if you have allocated the space that is returned by the function, make sure you delete it. Again, you have been warned.
Adrian