Declare a pointer to an array of pointers as extern

Hello everyone.
I'm trying to write a simulator. This simulator is maed of several .cpp files.

I hereafter try to explain my problem as clear as possible:

I have in the file "queue.h" (the classes buffer_in and buffer_xp are defined in "buffer.h" and "buffer.cpp"):

buffer_in **buf_in;
buffer_xp **buf_xp;

I have in the file "queue.cpp" (IN and OUT are defined):

buf_in = new buffer_in*[IN]();
buf_xp = new buffer_xp*[IN*OUT]();

So at this moment i should have created an array buf_in of pointers. Each pointer cointained in this array is itself a pointer which points to an object of class buffer_in.
In the same way, I've created and array buf_xp of pointers. Each pointer contained in this array is itself a pointer which points to an onject of class buffer_xp.

Now i have to able to modify these objects, both the buffer_in class and the buffer_xp class, from a different .cpp file.

Any hint i should declare them in the other .cpp file?
I don't know how to use extern in this case

Thanks in advance for the help.

Francesco
If I'm understanding you correctly...aren't you just looking for:
1
2
extern buffer_in** buf_in;
extern buffer_xp** buf_xp;
The extern keyword is used to tell the compiler that a data object is declared in a different *.cpp or *.c file (code unit). Its required for data objects but optional for function declarations. For example, you have two *.cpp files named A.cpp and B.cpp. B.cpp has a global int that needs to be used in A.cpp.
1
2
3
4
5
6
7
8
9
10
// A.cpp
#include <iostream>
// other includes here
...
extern int i; // this is declared globally in B.cpp

int foo()
{
i = 1;
}


1
2
3
4
5
6
7
8
9
10
11
// B.cpp
#include <iostream>
// other includes here
...
int i; // here we declare the object WITHOUT extern
extern void foo(); // extern is optional on this line

int main()
{
    foo();
}
Sorry, i thouhgt my error was generated by the declaration of an array of pointers as an extern. Actually adding

extern buffer_in** buf_in;
extern buffer_xp** buf_xp;

was enough (thanks Zhuge)

My error is of another type. Hope you can help me with this too. So...here it is:

1. I declare the two arrays of pointer

1
2
buffer_in **buf_in;
buffer_xp **buf_xp;


2. I create the two arrays of pointer (IN and OUT are defined):

1
2
buf_in = new buffer_in*[IN]();		
buf_xp = new buffer_xp*[IN*OUT]();


3. I try to operate on the objects created in the following way:

1
2
3
4
for (i=0; i<IN; i++)
{
	buf_in[i]->ingress_id=i;
}


where ingress_id is declared as below:

1
2
3
4
5
class buffer_in : public buffer {

public:
	int ingress_id;
};


When i try to run the line

buf_in[i]->ingress_id=i;

i get the error:

Non managed exception at 0x008b6294 in Simulator.exe: 0xC0000005:
violation in access of the writing of the path 0x00000018. (this is a translation, i m running Visual C++ in italian)

Any idea please what is this error due to?
I guess i refer to the buf_in[i] I am interested to in the wrong way...

Any help is very welcome. Thanks.

Francesco
Topic archived. No new replies allowed.