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

x
?
Solved

External Variable

Posted on 1998-07-22
5
Medium Priority
?
176 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
[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
5 Comments
 
LVL 11

Accepted Solution

by:
alexo earned 300 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

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…
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…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

670 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