I have a homework and we were given by the professor this main program I call it "MyMain.cpp"
#include <cstdlib>
#include <iostream>
using namespace std;
extern const int DIM0, DIM1, DIM2, DIM3;
extern float ***ptr4D[];
int main()
{
int value;
// Store all values.
value = 0;
for (int ix0 = 0; ix0 < DIM0; ++ix0)
for (int ix1 = 0; ix1 < DIM1; ++ix1)
for (int ix2 = 0; ix2 < DIM2; ++ix2)
for (int ix3 = 0; ix3 < DIM3; ++ix3)
ptr4D[ix0][ix1][ix2][ix3] = float(value++);
// Test all stored values.
value = 0;
for (int ix0 = 0; ix0 < DIM0; ++ix0)
for (int ix1 = 0; ix1 < DIM1; ++ix1)
for (int ix2 = 0; ix2 < DIM2; ++ix2)
for (int ix3 = 0; ix3 < DIM3; ++ix3)
if (ptr4D[ix0][ix1][ix2][ix3] != value++)
{
cout << "Error at: ptr4D[" << ix0 << "][" << ix1 << "][";
cout << ix2 << "][" << ix3 << "]\n";
return EXIT_FAILURE;
}
cout << "Success!\n";
return EXIT_SUCCESS;
}
when I do a "rebuilt" it says "unresolved External Symbol"
Note: that the declaration is not initialized, so I created another file called
SupportMain.cpp
and that is where I put the initialization.
const int DIM0 = 2, DIM1 = 3, DIM2 = 4, DIM3 = 5;
This is the recommendation of the other experience programmer when I search the web.
I rebuilt it again and , again I get a "unresolved External Symbol"
I am not to touch the MyMain.cpp, so any initialization cannot be done in MyMain.cpp
It should work isn't it. But obviously I am doing something wrong or do not know the problem using "extern const int".
A const in file scope has internal linkage: when the compiler builds your SupportMain.cpp, it doesn't make your consts visible externally. You have to make them external:
Normally, you'd keep your "extern" line in a header file (e.g. SupportMain.h), and you'd #include that header in both MyMain.cpp and SupportMain.cpp, and your SupportMain.cpp would otherwise be as you wrote.
But if you cannot modify MyMain.cpp, this will do.
Now define what p0 and p1 should be equal to. You have to follow the pattern and continue to define the memory associated with each array for it to work.
Yes, but you haven't allocated any memory to hold values.
What is this float ***ptr4D[] defining?
It is an array of triple pointers to floats. Now we know that we want to use this pointer
as a 4D array and the first dimension is DIM0 which equals 2. So, we have to set ptr4D to
have two elements, each of which is a triple pointer to floats.
If I use this as my SupportMain.cpp I get:
$ g++ SupportMain.cpp MyMain.cpp
SupportMain.cpp:5: error: storage size of ‘p0’ isn't known
SupportMain.cpp:6: error: storage size of ‘p1’ isn't known
$
So we have to give p0 and p1 memory to hold values. If ptr4D had DIM0 dimensions, how many should
p0 and p1 have?
The answer is DIM1.
***ptr4D[]
---------------------
2 | o | o |
--/--------------\---
/ \
--------------------- v **p1[]
/ -----------
v **p0[] | o | o | o |
----------- /----|----\
3 | o | o | o | ---------------- | --------
----------- / | \
etc. / | \
| v v
v *p10[] *p11[] *p12[]
--------------- etc. etc.
4 | o | o | o | o |
--------/------
/ etc.
/
------------
/
v float p102[]
---------------------------------------
5 | float | float | float | float | float | <--- At this point you have
--------------------------------------- memory to store a float
o are pointers
I tried your approach and I used a minimum of 24 arrays. And still I have a problem with "float ***ptr4D[] which is type float***[]. what I am getting from your approach is &ptr4D[0][0][0][0] whuich is type float * and the two are not compatible.
I read the book over and over and it seems there is a better approach which is very promising except I am still having a problem with the type float ***[].
I will close this topic and start using the new approach that I read, this is very promising.