Solved

MFC app error C2065: 'theApp' : undeclared identifier

Posted on 2016-07-26
4
38 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)
Comment Utility
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 32

Expert Comment

by:sarabande
Comment Utility
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 32

Assisted Solution

by:sarabande
sarabande earned 100 total points (awarded by participants)
Comment Utility
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 32

Expert Comment

by:sarabande
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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 …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

763 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now