Struct array access

G

Geky

Can you tell me how I can access to an array of structure declared in
a file .cpp from any class?
 
G

Geky

By name, usually.

V

I can't and I don't know why

example:

I declare an array of structure in a file. cpp.
I want to access to the structure from another file and then, in that
file, I declare the array of structure as "external".

when I try to use the structure my compiler returns me some errors
like:

error C2036: 'struct a_struct *': unknown size
error C2027: use of undefined type 'a_struct'
see declaration of 'a_struct'
error C2228: left of '. apple' must have class / struct / union type
Error executing cl.exe.

any idea?
 
G

Geky

By name, usually.

V

I can't and I don't know why

example:

I declare an array of structure in a file .cpp in a global class.
I want to access to the structure from another file in another class
and then, in that
file, I declare the array of structure as "external" like this

extern struct a_struct a_struct_ar[DIM_ARRAY];

when I try to use the structure my compiler returns me some errors
like:

error C2036: 'struct a_struct_ar *': unknown size
error C2027: use of undefined type 'a_struct_ar'
see declaration of 'a_struct_ar'
error C2228: left of '. apple' must have class / struct / union type
Error executing cl.exe.

any idea?
 
R

Robert Hairgrove

Victor said:
By name, usually.

V

You need to have an "extern" declaration in the other units. E.g.:

// foo.h
const int ARRAY_SIZE = 100;
struct MyStruct { /* whatever... */ };

// foo.cpp
#include "foo.h"
MyStruct MyStructArray[ARRAY_SIZE];

// bar.cpp
#include "foo.h"
extern MyStruct MyStructArray[ARRAY_SIZE];
 
V

Victor Bazarov

Geky said:
I can't and I don't know why

example:

I declare an array of structure in a file. cpp.
I want to access to the structure from another file and then, in that
file, I declare the array of structure as "external".

when I try to use the structure my compiler returns me some errors
like:

error C2036: 'struct a_struct *': unknown size
error C2027: use of undefined type 'a_struct'
see declaration of 'a_struct'
error C2228: left of '. apple' must have class / struct / union type
Error executing cl.exe.

any idea?

Oh, yes! See FAQ 5.8. You can find the FAQ Lite here:
http://www.parashift.com/c++-faq-lite/

It is *always* a good idea to check out the FAQ before posting.

V
 
N

Noah Roberts

when I try to use the structure my compiler returns me some errors
like:

error C2036: 'struct a_struct *': unknown size
error C2027: use of undefined type 'a_struct'
see declaration of 'a_struct'
error C2228: left of '. apple' must have class / struct / union type
Error executing cl.exe.

Your compiler doesn't know what a_struct looks like.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,265
Messages
2,571,069
Members
48,771
Latest member
ElysaD

Latest Threads

Top