Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

MFC app error C2065: 'theApp' : undeclared identifier

Posted on 2016-07-26
4
Medium Priority
?
155 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 25

Accepted Solution

by:
chaau earned 1600 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 35

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 35

Assisted Solution

by:sarabande
sarabande earned 400 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 35

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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
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 difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

705 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