Solved

External Variable

Posted on 1998-07-22
5
170 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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

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 …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 learn how to clear a vector as well as how to detect empty vectors in C++.

757 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

20 Experts available now in Live!

Get 1:1 Help Now