Solved

External Variable

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
cb: unreferenced local variable 11 72
Handle Exceptions during instantiation 28 411
C++ Language error 28 189
Header of docx file 17 97
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

911 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

16 Experts available now in Live!

Get 1:1 Help Now