Problem with variable type extern const

Hello again I am back,

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";

cout << "Success!\n";

when I do a "rebuilt" it says "unresolved External Symbol"

Note: that the declaration is not initialized, so I created another file called

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:

extern const int DIM0 = 2, DIM1 = 3, DIM2 = 4, DIM3 = 5;

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.
Last edited on
As for the ptr4D, you could start like this:
float **p0[] = // You fill in what this should be
float **p1[] = // You fill in what this should be

float ***ptr4D[] = {p0,p1};

I did what you suggested and put

extern const int DIM0 = 2, DIM1 = 3, DIM2 = 4, DIM3 = 5; in my SupportMain.cpp

and the "unresolved External Symbol" disappeared.

however when I did the same to the pointer,

extern float ***ptr4D[];

and put

"extern float ***ptr4D[]; " in SupportMain.cpp the "unresolved External Symbol" did not disappear.

does the extern pointer behaves different?

I did what you suggested and put these in SupportMain.cpp

float **p0[];
float **p1[];
float ***ptr4D[] = {p0,p1};

float **p0[];
float **p1[];

is causing "unresolved External Symbol"
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.

1. The pointer is declared in MyMain.cpp and the main program also in MyMain.cpp has a code to initialize the array.

extern float ***ptr4D[];
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.
float **p0[];
float **p1[];
float ***ptr4D[] = {p0,p1};
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.
Last edited on
This is what you want to create:
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

This is 2x3x4x5 or DIM0xDIM1xDIM2xDIM3

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.
Topic archived. No new replies allowed.