Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 192
  • Last Modified:

MFC app error C2065: 'theApp' : undeclared identifier

I have an MFC dialog based application in Visual Studio 2008, which we will call MyApp. It contains several other dialog/classes for specific purposes. In the MyApp.h file I do have extern MyApp theApp; declared at the bottom of the header file.

The problem is that all of the sub dialog classes will recognize the theApp global definition except one. For some reason I get a compile error "error C2065: 'theApp' : undeclared identifier" in this specifica dialog class. With the global definition present, and the other 6 dialog classes receognizing it just fine, for some reason this one does not. This dialog and class were created normally through the MFC, not imported as an 'existing item'.

I just don't understand why I have this problem in this particular dialog class. Any insight on this, and how I can resolve it would be appreciated.
0
TheIronDuke
Asked:
TheIronDuke
  • 3
2 Solutions
 
chaauCommented:
Your dialog has an include in the cpp or h file that is inserted before MyApp.h file where the theApp is defined. The included file (let's call it "someOtherFile.h") in turn has a pragma once of an ifdef guard in it that instructs the compiler to load it once.

Modify the cpp file of this dialog to make sure that the sequence of includes is as per the below:
#include "stdafx.h"
#include "MyApp.h"
#include "yourDialog.h"
#include "someOtherFile.h"

Open in new window

0
 
sarabandeCommented:
theApp is a variable declared as extern in your MyAppapp.h header file.

it needs an instantiation in one of the cpp files. normally you do it in MyAppapp.cpp where you simply add the statement

// the one and only MyApp application object
CMyApp theApp;

Open in new window


below include statements and above the first function body.

Sara
0
 
sarabandeCommented:
fyi: you also could remove the statement 'extern CMyApp theApp;' from header file as long as the 'theApp' variable was nowhere used in your code. the variable is only a helper such that you could easily access the singleton application object from all sources. however the AfxGetApp() would give a pointer to the application what is pretty much the same ease.

Sara
0
 
sarabandeCommented:
The answer from chaau is correct and should have solved the issue. There could be only one reason why an extern declared variable was not recognized in a cpp file (beside of a typo): the extern statement actually was not read from header file for some reason. that could happen if the header guard macro was used twice (the 2nd header than was not read) or by wrong use of the precompiled Header stdafx.h.

Sara
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now