Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Global Variables in MFC Application Wizard applications

Status
Not open for further replies.

dbharrison

Technical User
Jun 1, 2007
3
0
0
GB
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
Boatprog
 
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.
 
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.
Boatprog
 
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.
 
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 :-
CAboutDlg
CChildFrame
CMainFrame
CTestApp
CTestDoc
CTestView

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:-
{
A=1;
B=2:
Addup();
}

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
Boatprog

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top