Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Global Variables in MFC Application Wizard applications

Global Variables in MFC Application Wizard applications

Global Variables in MFC Application Wizard applications

I am currently working on a Peer to Peer Networking application created using Visual C++ 6 Application Wizard. The application uses AfxBeginThread to start a new thread and it is desireable to create the thread function outside the classes created using the Application Wizard, i.e. as a global function and using global variables(the reasons for this are to do with passing of parameters to the thread function).
I have not had to do this before and am having some difficulty with it. Again, using the Application Wizard I added a neww Source File and Header File to the project. I placed the thread function in the source file and the data declarations in the header file and, as expected they showed up in the Class window as Globals and should therefore be accessible from any of the classes (the CView class in this case) but when I try to compile the program both the global function and the global variables show up as "undeclared identifiers" in the view class, error C2065.
If I include the header file inside the CView source file then the variables are shown as being declared twice and again the compilation fails
Have I missed something here?
Thanks for any help available

RE: Global Variables in MFC Application Wizard applications

If I followed this correctly, you have the variables declared within CView class to be passed into the thread Class that you created. In that case, you should have differently named class level variables that are assigned within the constructor.

RE: Global Variables in MFC Application Wizard applications

Thankyou macleod for responding. The variables are not declared in the Cview class. Like the Thread Function they are declared outside the classes. Two files were created outside the Classes, a source file and a header file. The source file contains the Thread Function and the Header file contains the variables. I hope that this makes the Thread function and the Variables global and therefore accessible from anywhere within the classes.
Obviously I have got something wrong somewhere.

RE: Global Variables in MFC Application Wizard applications

The header file is included in the source file, right? When you declare the variable in the header file, it makes it available within the class...just not initialized yet.

So, if you don't have the virtual keyword on the variable names in the parent class, then it will show up as being declared twice.

Can you include the pertinent parts of the header and source files for both classes? That would give a clearer picture of what's going on.

RE: Global Variables in MFC Application Wizard applications

Hi Macleod
I dont think I have managed to convey what I am trying to do so I have produced the simplest programme possible, using the Application Wizard, which leaves aside all the Multithreading stuff, which is irrelevant to the problem. The problem is still there, however.
The project is called Test and starts with the following classes :-

To these I have added, using the Application Wizard, two files Send.h and Send.cpp. Neither of these files is part of a Class but they have both been added to the project.
Send.h contains the function prototype and variable declarations as follows :-
void Addup();
int A,B,C;

Send.cpp contains the function body together with the include statements as follows:-
include "StdAfx.h"
include "Send.h"
void Addup()
   A = B+C;

The Class Window now lists both the funtion Addup and the variables A,B and C under the Global heading, which if my understanding is correct, should make them accessible from anywhere in the program, including from inside any of the classes

I have then added an OnRun function to the CTestDoc Class which can be activated by the user from a menu and this function does:-

Variables A,B and C are not declared in TestDoc.h and Send.h is not included in TestDoc.h
Compilation shows A,B and Addup as Undeclared Identifiers from TestDoc.cpp

hope you can follow this

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close