Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!cornell!uw-beaver!fluke!ssc-vax!dmg From: dmg@ssc-vax.UUCP (David Geary) Newsgroups: comp.lang.c++ Subject: Help! Keywords: constructors, C++ i/o, extern Message-ID: <2774@ssc-vax.UUCP> Date: 5 Jul 89 18:08:24 GMT Organization: Boeing Aerospace Corp., Seattle WA Lines: 74 Here's something interesting: First, the file works.cxx: ---------------------------------------------------------------------- #include "//ernie/user/geary/c++/include/stream.hxx" // c++ i/o class junk { public: junk() { puts("Constructing Junk"); } }; junk myjunk; main() { } ---------------------------------------------------------------------- When I compile and run, I get: $ works Constructing Junk $ (In other words, everything is ok). However, watch this: The file noworks.cxx: ---------------------------------------------------------------------- #include "//ernie/user/geary/c++/include/stream.hxx" // c++ i/o class junk { public: junk() { cout << "Constructing Link\n"; } }; junk myjunk; main() { } ---------------------------------------------------------------------- When I compile and run, I get: $ noworks Segmentation Fault $ (In other words, everything is NOT ok ;-). The only difference between the two is that I use puts() in works.cxx, and use cout << in noworks.cxx. How, watch this: The file worksagain.cxx ---------------------------------------------------------------------- #include "//ernie/user/geary/c++/include/stream.hxx" // c++ i/o class junk { public: junk() { cout << "Constructing Link\n"; } }; main() { junk myjunk; } ---------------------------------------------------------------------- When I compile and run, I get: $ worksagain Constructing Junk $ So now, I can use cout << ... but I cannot have the object myjunk external, it has to be in main(). Can anybody provide an explanation as to why this is happening? -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~ David Geary, Boeing Aerospace, Seattle ~ ~ "I wish I lived where it *only* rains 364 days a year" ~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~