Struct array access

Discussion in 'C++' started by Geky, Feb 16, 2010.

  1. Geky

    Geky Guest

    Can you tell me how I can access to an array of structure declared in
    a file .cpp from any class?
    Geky, Feb 16, 2010
    #1
    1. Advertising

  2. Geky wrote:
    > Can you tell me how I can access to an array of structure declared in
    > a file .cpp from any class?


    By name, usually.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Feb 16, 2010
    #2
    1. Advertising

  3. Geky

    Geky Guest

    On 16 Feb, 15:58, Victor Bazarov <> wrote:
    > Geky wrote:
    > > Can you tell me how I can access to an array of structure declared in
    > > a file .cpp from any class?

    >
    > By name, usually.
    >
    > V
    > --
    > Please remove capital 'A's when replying by e-mail
    > I do not respond to top-posted replies, please don't ask


    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?
    Geky, Feb 16, 2010
    #3
  4. Geky

    Geky Guest

    On 16 Feb, 15:58, Victor Bazarov <> wrote:
    > Geky wrote:
    > > Can you tell me how I can access to an array of structure declared in
    > > a file .cpp from any class?

    >
    > By name, usually.
    >
    > V
    > --
    > Please remove capital 'A's when replying by e-mail
    > I do not respond to top-posted replies, please don't ask


    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?
    Geky, Feb 16, 2010
    #4
  5. Victor Bazarov wrote:
    > Geky wrote:
    >> Can you tell me how I can access to an array of structure declared in
    >> a file .cpp from any class?

    >
    > 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];
    Robert Hairgrove, Feb 16, 2010
    #5
  6. Geky wrote:
    > On 16 Feb, 15:58, Victor Bazarov <> wrote:
    >> Geky wrote:
    >>> Can you tell me how I can access to an array of structure declared in
    >>> a file .cpp from any class?

    >> By name, usually.
    >>
    >> V
    >> --
    >> Please remove capital 'A's when replying by e-mail
    >> I do not respond to top-posted replies, please don't ask

    >
    > 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
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Feb 16, 2010
    #6
  7. Geky

    Geky Guest


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




    heyyyy >.< Stay calm
    Geky, Feb 16, 2010
    #7
  8. Geky

    Geky Guest

    On 16 Feb, 17:34, Victor Bazarov <> wrote:
    > Geky wrote:
    > >> It is *always* a good idea to check out the FAQ before posting.

    >
    > > heyyyy >.< Stay calm

    >
    > <shrug> Stay ignorant


    ahahahhaha ok ok funny guy ;-)
    Geky, Feb 16, 2010
    #8
  9. Geky

    Noah Roberts Guest

    In article <2030dcbe-3aeb-4322-8f50-
    >, says...

    > 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.

    --
    http://crazyeddiecpp.blogspot.com
    Noah Roberts, Feb 17, 2010
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris Fogelklou
    Replies:
    36
    Views:
    1,371
    Chris Fogelklou
    Apr 20, 2004
  2. Ole
    Replies:
    4
    Views:
    595
    Michael Wojcik
    Oct 26, 2004
  3. Replies:
    3
    Views:
    1,080
  4. castironpi

    struct.Struct random access

    castironpi, Aug 26, 2008, in forum: Python
    Replies:
    4
    Views:
    298
    castironpi
    Aug 30, 2008
  5. Tuan  Bui
    Replies:
    14
    Views:
    470
    it_says_BALLS_on_your forehead
    Jul 29, 2005
Loading...

Share This Page