Advertisement

dumbass extern question...

Started by June 28, 2000 12:57 AM
4 comments, last by gimp 24 years, 5 months ago
Following a little example from the other day to create an output log for my application I tried to allow the stream to be gflobal to all files in the application. In a common header file I have: extern ofstream Output; There is a matching "ofstream Output;" in an initialization routine. I also have iostream and fstream headers included in all .cpp files. I however get the following: Config.obj : error LNK2001: unresolved external symbol "class ofstream Output" (?Output@@3Vofstream@@A) Winsock.obj : error LNK2001: unresolved external symbol "class ofstream Output" (?Output@@3Vofstream@@A) System.obj : error LNK2001: unresolved external symbol "class ofstream Output" (?Output@@3Vofstream@@A) Managers.obj : error LNK2001: unresolved external symbol "class ofstream Output" (?Output@@3Vofstream@@A) Is there something I''m missing? I''ve done this before but somehow it doesn''t work this time. gimp
Chris Brodie
One question, is the definition of the object in a routine (i.e. function or method) OR is it defined outside...

In order for extern to work the object (or variable) needs to be defined outside of any function and have global scope. That is the whole purpose of extern. If the object is defined inside a class or function is has local scope to that class or function and therefore not global.

//Someheader.h

extern MyClass MyObject

//Source1.cpp

#include "Someheader.h"

int main()
{
MyObject.DoSomething();
}

//Source2.cpp

#include "Someheader.h" //Does not need to be included for this example.. but if you have other things defined it is a good idea

MyClass MyObject; //Object defined outside of any routine or class

Hope this helps
-Omalacon

Edited by - Omalacon on June 28, 2000 2:33:36 AM
Advertisement
Can you use extern within namespaces?

...not that I want to, just curious...
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
quote: Original post by Magmai Kai Holmlor

Can you use extern within namespaces?

...not that I want to, just curious...


Being that you can easily try this at home with a compiler of your choosing to answer your own question, I won''t answer it for you.

However, I will say this. Pretend that you are a compiler and you go through your source a line at a time. Examine each line of your code and without making assumptions see if each line makes sense. i.e.
Does the statement contain "X"?
Does "X" have a declaration? If not, is it being declared?
Does "X" have a type?
Is "X" being used in a logical way that follows it''s declaration?
Does "X" follow all logical scoping rules?
etc...

If you can answer these questions, you would know without having to see if it will compile.



YAP YFIO,
deadlinegrunt

~deadlinegrunt

opps... you might have been curious but you also highlighted a problem .... maybe thats what is wrong... I''ve only namespace''ed in one file....doh!
Chris Brodie
Err... I''ve now got a common header file that include headers that are global to the project like windows.h, string etc. This also defines the namespaces I use for the whole project(std)

Now I get :

c:\program files\microsoft visual studio\vc98\myprojects\listserver\common.h(20) : error C2872: ''ofstream'' : ambiguous symbol

ambiguous? What does this error mean?

    #ifndef COMMON_H#define COMMON_H#ifdef WIN32#define WIN32_LEAN_AND_MEAN#include <windows.h>#endif WIN32#include <stdio.h>#include <stdlib.h>#include <iostream>#include <fstream>#include <string>#include <queue>#include "System.h"//#include "Utilities.h"using namespace std;extern ofstream Output;#endif COMMON_H    


This value is initialized in main.cpp and used throughout the application. In each file it''s used in I get the error above.

Thanks

Chris
Chris Brodie

This topic is closed to new replies.

Advertisement