Global struct in MFC app

This is a discussion on Global struct in MFC app within the Windows Programming forums, part of the Platform Specific Boards category; Hello, I have an MFC application that has many different classes which need to access a struct filled with particular ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    34

    Global struct in MFC app

    Hello,
    I have an MFC application that has many different classes which need to access a struct filled with particular values. All of the classes need to have access to these values so I thought that this would be an opportunity to use a global variable in a decent way. However I put the struct declaration
    Code:
    struct some_struct{
    int one;
    int two;
    int three;
    };
    in the
    Code:
    class CMyApp : public CWinApp
    header and then make an instance of that struct in the implementation file
    Code:
    some_struct instanceOfStruct;
    However,
    Code:
    extern some_struct instanceOfStruct;
    is only working for me in a few classes and other times it will tell me,
    Code:
    error C2146: syntax error : missing ';' before identifier 'instanceOfStruct'
    error C2065: 'instanceOfStruct' : undeclared identifier
    Any thoughts? I know this is poorly worded, and I apologize for that, but I'm just too tired to try and fix it right now. Thank you for any help you might be able to give me. Also, criticism and better ways to do this are welcome.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    It sounds like, based on what I see here, that some files don't recognize the name some_struct as a typename. Do all of these files include the appropriate header?

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    214
    Make the "global" variable/struct/whatever a member of the application class. Then anywhere in your code you can access it using AfxGetApp().

    I don't know if this technique is frowned upon, or bad practice. It worked for me.

  4. #4
    EOP
    EOP is offline
    Registered User
    Join Date
    Jan 2008
    Posts
    45
    globals.h

    Code:
    #ifndef GLOBALS_H
    #define GLOBALS_H
    
    struct some_struct
    {
    	int one;
    	int two;
    	int three;
    };
    
    class CGlobals
    {
    public:
    
    	static some_struct instanceOfStruct;
    	
    	// friend class some_class;
    
    #endif
    
    extern CGlobals globals;
    globals.cpp
    Code:
    #include "globals.h"
    
    CGlobals globals;
    
    int CGlobals::instanceOfStruct.one = 1;
    ...

    access with:
    Code:
    #include "globals.h"
    
    globals.instanceOfStruct

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. global struct variable best use
    By Kempelen in forum C Programming
    Replies: 2
    Last Post: 06-05-2009, 05:08 AM
  2. non-MFC DLL with MFC app question.
    By Kempelen in forum Windows Programming
    Replies: 10
    Last Post: 08-20-2008, 07:11 AM
  3. Client - Server TCP/IP MFC app..... Help!
    By amedinadiaz in forum C++ Programming
    Replies: 0
    Last Post: 10-26-2005, 11:57 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Bi-Directional Linked Lists
    By Thantos in forum C Programming
    Replies: 6
    Last Post: 12-11-2003, 09:24 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21