Console C++ - iostream Template Namespace
iostream is supposed to be the operating system independent way
to read and write data.
In general, this works pretty good if you include iostream.h.
However, if you use the new standard template
#include // Notice, no .h
there is a major difference.
According to the 1998 C++ standard, when any of the new C++ Standard Library classes
are used (such as <iostream>),
the following command is required in order to actually
use the applicable commands.
using namespace std;
However, in Borland C++ 5.02,
it does not work.
(It was fixed in version 5.5.)
For a work around so that your code will work with other compilers,
Microsoft vs. Borland Portability Woes
presents
#if !defined(__BORLANDC__)
using namespace std;
#endif
or
#ifndef __BORLANDC__
using namespace std;
#endif
Another solution (workaround)
is to add a line which defines (or extends) the std namespace
before your using statement.
namespace std {}
using namespace std;
Alternate Syntax
Well the kludges above worked ok until the teacher required the following constructs.
#include // Notice, no .h
std::ostream& operator << (std::ostream &, const Item & );
std::istream& operator >> (std::istream & ins, Item & );
std::ostream& operator <<(std::ostream& outs, const Item & strucItem)
{
outs << strucItem.number;
return outs;
}
I couldn't find any simple way to handle this.
The following is my best suggestion.
#include // Provides ostream and istream
#ifndef __BORLANDC__
std::ostream& operator << (std::ostream &, const Item & );
std::istream& operator >> (std::istream & ins, Item & );
#else
ostream& operator << (ostream &, const Item & );
istream& operator >> (istream & ins, Item & );
#endif
// This implements the function
#ifndef __BORLANDC__
std::ostream& operator <<(std::ostream& outs, const Item & strucItem)
#else
ostream& operator <<(ostream& outs, const Item & strucItem)
#endif
{
outs << strucItem.number;
return outs;
}
I know I could have defined an alias (macro) for std::
and used that to reduce the number of if statements.
However,
I don't think that that would be as clear for a beginning course.
Author: Robert Clemenzi -
clemenzi@cpcug.org