Solved

MFC app error C2065: 'theApp' : undeclared identifier

Posted on 2016-07-26
4
94 Views
Last Modified: 2016-08-22
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
Comment
Question by:TheIronDuke
  • 3
4 Comments
 
LVL 24

Accepted Solution

by:
chaau earned 400 total points (awarded by participants)
ID: 41730514
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
 
LVL 33

Expert Comment

by:sarabande
ID: 41730672
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
 
LVL 33

Assisted Solution

by:sarabande
sarabande earned 100 total points (awarded by participants)
ID: 41730758
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
 
LVL 33

Expert Comment

by:sarabande
ID: 41756102
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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question