Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

External Variable

Posted on 1998-07-22
5
Medium Priority
?
177 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 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
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 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.
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.
Suggested Courses

916 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