tom_usenet <> wrote in message news:<>. ..
> On 20 Jul 2004 07:26:10 -0700, (Omid) wrote:
>
> >cl.exe = Microsoft (R) 32-bit C/C++ Optimizing Compiler Version
> >12.00.8168 for 80x86
>
> What version is that in English? VC 6?
Yes, that's correct. It's VC 6.0.
I should have mentioned that.
> >Can someone please tell me how to make my code work also with the old
> >headers.
>
> Which old headers? The old headers are non-standard, and differ in
> meaning depending upon the exact compiler you are using. I'll assume
> you are using Microsoft's old headers.
>
> In that case, you can't write code that works both for the old and new
> headers, but here is some code that I suspect will work with the old
> headers. Some old iostreams libraries had a class ostream_with_assign,
> and if yours is one of them, this is what you need (untested since I
> don't have any compilers that are old enough to compile it):
>
> #include <iostream.h>
> #include <fstream.h>
>
> int main ()
> {
> cout << "This is sent to prompt" << endl;
> ofstream file;
> file.open ("test.txt");
> ostream_with_assign oldcout = cout;
> cout = file;
> cout << "This is sent to file" << endl;
> cout = oldcout;
> cout << "This is also sent to prompt" << endl;
> return 0;
> }
Thanks a lot for your help. This was what I was looking for!
However, your "ostream_with_assign" should be changed to
"ostream_withassign", otherwise it's all correct and I'm able to
compile and link this file. So far everything is fine, but when I
execute the exe-file the program crashes and I get this error message:
The instruction at "0x00340905" referenced memory at "0x006811e8". The
memory could not be "written".
(The error is shown in the "standard Windows error window" with an ok
and cancel box.)
The program output:s "This is sent to prompt" and the test.txt is
created with the content "This is sent to file".
But the last message "This is also sent to prompt" isn't sent to
STDOUT.
Does anyone have any idea why this happens?
The compilation and linking is successful,
but during the execution I get this error.
I have also tried several modification of the code above.
One of them that is shown below can successfully be executed with
VC++,
but when I tried to compile exactly the same program with Cygwin g++,
it does not compile.
I'm only using standard headers now.
Why is it possible that it works with VC but not with G++?
Code:
//WORKS FINE WITH CL.EXE (VC++ 6.0)
//BUT NOT WITH CYGWIN G++
//################################
#include <iostream>
#include <fstream>
using namespace std;
class ostream_withassign : public ostream {
public:
ostream_withassign() : ostream((streambuf*)0) {}
~ostream_withassign() {}
ostream_withassign& operator=(ostream& __s) {
ios::init(__s.rdbuf());
return *this;
}
ostream_withassign& operator=(streambuf* __s) {
ios::init(__s);
return *this;
}
};
int main ()
{
cout << "This is sent to prompt" << endl;
ofstream file;
file.open ("test.txt");
ostream_withassign oldcout; //Split into 2 lines
oldcout= cout; //Split into 2 lines
cout = file;
cout << "This is sent to file" << endl;
cout = oldcout;
cout << "This is also sent to prompt" << endl;
return 0;
}
//################################
Note that I now use the standard header files (without .h).
Error using Cygwin g++ (3.3.1):
################
$ g++ mycout.cpp
/usr/include/c++/3.3.1/bits/ios_base.h: In member function `
std::basic_ios<char, std::char_traits<char> >& std::basic_ios<char,
std::char_traits<char> >:

perator=(const std::basic_ios<char,
std::char_traits<char> >&)':
/usr/include/c++/3.3.1/bits/ios_base.h:671: error: `std::ios_base&
std::ios_base:

perator=(const std::ios_base&)' is private
mycout.cpp:73: error: within this context
################
For a while I asked myself, is it possible that the problem
that the first program didn't work is in the .h-header files?
First I thought that made sense, but then I tried to compile
this program (using headers without ".h").
//WORKS WITH CYGWIN G++
//BUT NOT WITH CL.EXE (VC++) (compiles, but error when executed)
//################################
#include <iostream>
#include <sstream>
using namespace std;
int main ()
{
int val;
string mystr;
stringstream ss (stringstream::in | stringstream:

ut);
ss << "120 42 377 6 5 2000";
cout.rdbuf(ss.rdbuf());
for (int n=0; n<6; n++)
{
ss >> val;
cerr << val*2 << '\n';
}
cerr << "Hello World! " << '\n';
return 0;
}
//################################
This compiles and links without problem, and when executed all output
is sent to STDERR (also the last "Hello World" is outputted), but then
the program crashes with this error message:
The instruction at "0x00402f7f" referenced memory at "0x0000000sd".
The memory could not be "read".
The program does work fine Cygwin g++ (3.3.1)! But not with VC 6.0.
(compiled with "g++ test.cpp" and "cl /GX /TP test.cpp" (also compiled
with the entire development VC GUI, but with the same result)).
So now I don't know, is there a problem with my C++-code, or can my VC
be corrupt? Or is there some other explanation?
Once again, thanks to all of you helping me out with this!
Any help, ideas, clues or information is very much appreciated.
/Omid