Solved

External Variable

Posted on 1998-07-22
5
172 Views
Last Modified: 2010-04-01
Hi,
I'm using MS VC++ 5.0 to program an SDI with a custom dialog box containing an edit box.  I've declared the new class for the dialog box & declared a variable associated with the edit box.

I need to display data obtained in the OnKeyDown function from the View class, into the (disabled) edit box - which is, of course, in the Dialog class.  Since I need a new variable available to both classes. I thought I'd use an external variable in a new header file & #include in both class .cpps.

However, I get an "unresolved external symbol" error!  Any ideas?

Regards,

Chris J robinson
0
Comment
Question by:chrisrobinson
  • 3
5 Comments
 
LVL 11

Accepted Solution

by:
alexo earned 100 total points
ID: 1168275
You have to declare the variable somewhere.

Example:

FILE.H:
    extern int putz; // declaration

FILE1.CPP:
    #include "file.h"

FILE2.CPP:
    #include "file.h"

FILE3.CPP:
    #include "file.h"
    int putz; // definition

0
 
LVL 2

Expert Comment

by:VEngineer
ID: 1168276
Alexo's got the point, but you don't have to make a seperate file and it is not very good to put a global variable in a header file (it only makes things more complex).

In one cpp file (let's say the dialog one), DEFINE the variable globally (outside of all functions):

int foo;  // globals are automatically init to zero

In any other file you want to use this variable in (the view class), DECLARE the variable globally with the keyword extern to indicate that the variable exists somewhere outside of the file:

extern int foo;

0
 
LVL 11

Expert Comment

by:alexo
ID: 1168277
>> but you don't have to make a seperate file and it is not very good to put a global variable in a header file.

Huh???

What separate file?  The include file is there because in real life there are usually more than one extern variable.  It is assumed that all three source files use the variable.

Who put the variable in the header file?  Certainly not me!

0
 

Author Comment

by:chrisrobinson
ID: 1168278
Thanks guys!  I was trying to avoid using a global, since it seems undesirable in C++.  Would the external variable alternative work? Which is better?
0
 
LVL 11

Expert Comment

by:alexo
ID: 1168279
chrisrobinson, the "external variable alternative" *is* using a global.
It just says that the global is defined in another translation unit (a .CPP file in your case).
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

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…
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 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 user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

790 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