Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Compile error with iostream, but not iostream.h for << & >> operator 1

Status
Not open for further replies.

Caphriel

Technical User
Feb 19, 2005
3
0
0
US
I have a persistent problem with my compiler (Microsoft Visual C++ 6.0). Whenever I override the extraction and insertion (<< and >>) operators in a header file for a class, I get errors when I try to compile the .cpp file, but only if I am using the iostream header file. The errors don't appear when I use the iostream.h header file.

The same code will compile fine on other computers, so I assume it's a compiler setting, but nobody I've asked knows what it is.

Does anyone know what the setting is that needs to be changed so that these lines of code:

Code:
friend ostream& operator <<
friend istream& operator >>

will compile properly?

Some of the error messages include:

error C2433: 'ostream' : 'friend' not permitted on data declarations
error C2501: 'ostream' : missing storage-class or type specifiers
error C2244: 'ostream' : unable to resolve function overload
error C2061: syntax error : identifier 'ostream'
error C2501: '<<' : missing storage-class or type specifiers

Thank you.
 
> Compile error with iostream, but not iostream.h
Probably because your OLD VC++ compiler doesn't understand new-style C++ header file conventions.

--
 
Caphriel said:
Code:
friend ostream& operator <<
friend istream& operator >>
Those lines of code will never compile properly, regardless of compiler settings.

Do you still get the error when you add the parameter lists and semicolons to those declarations?
 
Yes, the errors still occur with parameter lists and semicolons. I omitted them from the code sample because the errors occur regardless.
 
Not at a pc with vc++ atm, but the error messages sound very much like those you would get with no

using namespace std

Some compilers still polutte the global namespace with the newer c++ headers (as well as putting things in std), which would explain why it works fine on some other machines.
 
Adding a "using namespace std;" to the header file did solve the problem. Thank you.
 
No worries. Though generally i try to avoid putting "using namespace xxx" in header files, as then any file that includes the header also gets the whole namespace. Best off quantifying the namespace explicitly where needed,

friend std::eek:stream& operator << (/*...*/);
friend std::istream& operator >> (/*...*/);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top