Global Structures

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jj555s
    New Member
    • Mar 2007
    • 36

    #1

    Global Structures

    I've been trying use use a Global Struct however I get this eeror message:

    error C2236: unexpected 'struct' 'my_struct'


    my code (MyStruct.h)

    struct my_struct {
    int box2;
    } extern struct my_struct boxedit;
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    you forgot the ; after the } which terminates the struct
    Code:
    struct my_struct {
    int box2;
    };
    extern struct my_struct boxedit;

    Comment

    • jj555s
      New Member
      • Mar 2007
      • 36

      #3
      i think i did that actually and it gave me the same error

      Comment

      • Roonie
        New Member
        • Mar 2007
        • 99

        #4
        Code:
        struct my_struct {
        int box2;
        };
        extern struct my_struct boxedit;
        isnt that trying to declare my_struct twice? (or is this a C thing again?) i would say - though im just guessing - to try this:
        Code:
        struct my_struct {
        int box2;
        };
        extern my_struct boxedit;

        Comment

        • jj555s
          New Member
          • Mar 2007
          • 36

          #5
          o nevermind it works

          Comment

          • horace1
            Recognized Expert Top Contributor
            • Nov 2006
            • 1510

            #6
            Originally posted by Roonie
            Code:
            struct my_struct {
            int box2;
            };
            extern struct my_struct boxedit;
            isnt that trying to declare my_struct twice? (or is this a C thing again?) i would say - though im just guessing - to try this:
            Code:
            struct my_struct {
            int box2;
            };
            extern my_struct boxedit;
            yes, its a C thing! your version is OK in C++ but not C

            however, in C you can achieve a similar thing using typedef
            Code:
            typedef struct  {
            int box2;
            } my_struct;
            extern my_struct boxedit;

            Comment

            • jj555s
              New Member
              • Mar 2007
              • 36

              #7
              I'm not too sure how to work this

              every instance i make, it assumes it as local

              Comment

              • jj555s
                New Member
                • Mar 2007
                • 36

                #8
                VIT error LNK2001: unresolved external symbol "struct my_struct boxedit" (?boxedit@@3Umy _struct@@A)

                Comment

                • jj555s
                  New Member
                  • Mar 2007
                  • 36

                  #9
                  I'm trying to have 1.cpp and 2.cpp share the struct, for example int box; but I'm having issues with 2.cpp saying it's not defined before it being used?

                  Comment

                  • horace1
                    Recognized Expert Top Contributor
                    • Nov 2006
                    • 1510

                    #10
                    Originally posted by jj555s
                    I'm not too sure how to work this

                    every instance i make, it assumes it as local
                    not sure what the problem is, if you have a header file my_struct.h
                    Code:
                    struct my_struct {
                    int box2;
                    };
                    extern struct my_struct boxedit;
                    you need to define boxedit in one file, e.g. file1.c
                    Code:
                    #include "my_struct.h"
                    
                    struct my_struct boxedit;  // definition
                    
                    void setup()
                    {
                        boxedit.box2=10;   // setup a value
                    }
                    and the use it elsewhere, e.g. file2.c
                    Code:
                    #include "my_struct.h"
                    
                    int main()
                    {
                        setup();
                        printf("%d\n", boxedit.box2);
                        system("pause");
                    }
                    you then need to compile and link the files, e.g.
                    g++ file1.c file2.c

                    Comment

                    • jj555s
                      New Member
                      • Mar 2007
                      • 36

                      #11
                      Is void setup an automatic function? I'm using MFC for this progream

                      Comment

                      • Roonie
                        New Member
                        • Mar 2007
                        • 99

                        #12
                        wait . . .

                        youre using MFC and C together? you said your files are named *.cpp, right? please help me out a little: what language are you writing in?

                        (and if you could post a little more code, that would be excellentemente hente)

                        Comment

                        Working...