Solved

MFC app error C2065: 'theApp' : undeclared identifier

Posted on 2016-07-26
4
79 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

785 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