CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jan 2010
    Posts
    45

    Question How to use Globals

    Hi,

    I'm trying to make a type definition and a vector (based on that type definition) global to all my forms in vc++ and it isn't working.

    I have one file named "Brians_Globals.h" which is as follows:
    Code:
    #ifndef NAMESPACE_BRIAN
    
    #define NAMESPACE_BRIAN
    
    
    extern int id;
    
    extern int credit;
    
    
    extern struct CustomerRecords
    {
    	std::string Building;
    	std::string Notes;
    	std::string PrimaryContact;
    	std::string PrimaryNumber;
    	std::string SecondaryContact;
        std::string SecondaryNumber;
    	std::string Address1;
    	std::string Address2;
    	std::string City;
    	std::string State;
    	std::string ZipCode;
    	std::string ClientSince;
    	std::string Rate;
    	std::string PerCycle;
    	std::string PaymentDue;
    };
    
    extern typedef pair<std::string, struct CustomerRecords> my_pair;
    extern vector<my_pair> data;
    
    
    extern std::string bri;
    
    
    #endif
    and a file named "Brians_Globals.cpp" as follows:
    Code:
    #include "stdafx.h"
    #include <string>
    
    
    using namespace std;
    
    string bri;
    
    struct CustomerRecords;
    typedef my_pair;
    vector<my_pair> data;
    
    int id;
    int credit;
    Then I use:
    Code:
    #include "Brians_Globals.h"
    in any of the forms in the vc++ project application that need access to the globals as necessary.

    When I comment out both the
    Code:
    typedef my_pair;
    vector<my_pair> data;
    from the "Brians_Globals.cpp" file and the

    Code:
    extern typedef pair<std::string, struct CustomerRecords> my_pair;
    extern vector<my_pair> data;
    from the "Brians_Globals.h" file, then everything compiles and the globals can be used in my form files as desired, but if the above aren't commented out, then many, many errors occur. Thus I know that the
    Code:
    typedef my_pair;
    vector<my_pair> data;
    from the "Brians_Globals.cpp" are declared wrong. How should I declare them in the file?
    Last edited by vc_user; February 10th, 2011 at 07:01 PM.

  2. #2
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: How to use Globals

    typedefs are defines, and don't need to be extern.

    the typedef in the .h file (without the extern) should be enough.
    the typedef in the .cpp file is wrong.

  3. #3
    Join Date
    Jan 2010
    Posts
    45

    Re: How to use Globals

    Hi thanks for your help,

    I removed the "extern" from the .h file and removed the typedef statement altogether from the .cpp file and now I'm getting the following error:

    Code:
    C2143: syntax error : missing ';' before '<'
    although there is no missing semi-colon.

  4. #4
    Join Date
    Jan 2010
    Posts
    45

    Re: How to use Globals

    Thanks for your help. I just figured it out. There was a missing "std::" statement.

    Thanks again!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured