2

I am using two compilers g++ and Dev - C++. when I compile my program on Dev-C++ it compiles perfectly. but when i try to compile it on g++ it gives me two errors:

In file included from a2test.cpp:27:
----.h:25: error: 'ostream' has not been declared
----.h:26: error: 'istream' has not been declared

Can anyone tell me what can I do to solve this problem.

Thanks

Billy ONeal
  • 104,103
  • 58
  • 317
  • 552
  • Well, is at least one header which defines those included? – Billy ONeal Mar 03 '11 at 04:03
  • It's vaguely possible that the Dev C++ headers are old and either put those objects in the global namespace or have some `using` statement for them or namespace `std`. Ugly. It's also possible that the includes have a different heirarchy, and you're just expecting those objects to be declared because you're including some other header that happened to use them on Dev-C++. As Billy says, check you do actually have an `#include ` or `` statement. In desperation, running just the preprocessor stage of the compiler (`gcc -E`) often gives useful insight into what's included. – Tony Delroy Mar 03 '11 at 04:09

3 Answers3

5

Make sure you include fstream. Also, put "std::" before ostream or put "using namespace std" somewhere.

It would help if you posted the code, as right now I'm just guessing based on common mistakes.

I would guess you forgot to include fstream because different compilers may use different header files and it may be the case that g++ has a header file with

// iostream
#include <fstream>

While Dev-C++ may have

// iostream
// no include for fstream in this file

So you're accidentally importing the correct header file rather than doing it explicitly.

For header files, I just use this site when I forget which one.

ostream - C++ Reference

It seems you need to include ostream to get ostream. Probably the same thing for istream.

Jonathan Sternberg
  • 6,421
  • 7
  • 39
  • 58
  • 2
    The errors are for `istream` and `ostream` - available from `` and `` or together from `` (declarations) or `` (definitions), giving no reason to include `` specifically (which contains classes derived from `istream` and `ostream` and will therefore include their definitions as well as other potentially unnecessary code). – Tony Delroy Mar 03 '11 at 04:13
2

My psychic debugging skills indicate that the problem likely means that your call to g++ and the g++ Dev-CPP is using are different versions of gcc. One of the headers in the (presumably earlier) version included with Dev-CPP might #include a standard C++ header that it doesn't need to, which would allow headers which aren't strictly correct to compile.

Make sure you've actually #included <iostream>, or <istream> and <ostream>, or <iosfwd> -- some header which actually includes these types for you.

(Side Note: Please don't use Dev-CPP -- the project is pretty much dead, and the editor commits quite a few sins. Plus it isn't a good editor anyway. How about Code::Blocks or Visual Studio (both free) instead?)

Billy ONeal
  • 104,103
  • 58
  • 317
  • 552
0

dont know if this will help, but firstly, yuou should remember to omit the ".h" that some other compilers (MS-C++) use, but not ANSI/G++.so it should be just

#include <iostream>

Secondly, don't forget :

using namespace std;

3rdly, it's been a long time, but if I remember correctly, in g++, th istream and ostream functions are in the "std" library .. so you can do something like this :

using std::istream;
//later
istream::iostate state = ...

or alternatively, you can use them directly like this :

std::istream::iostate state = ...

Hopefully that'll give you some ideas.

IM.
  • 663
  • 2
  • 6
  • 15
  • 1
    MS-C++ does not add the `.h`. It will **accept** such code with a warning, just as GCC will. Also, G++ is no more ANSI than is MSVC++. – Billy ONeal Mar 03 '11 at 04:17
  • 1
    If I remember correctly, g++ would nto accept #include , besides, you're missing the whole point, which is using the std library. – IM. Mar 03 '11 at 04:34
  • Hmm.. it seems you are correct (as of GCC 4.4.3). While you see that as "missing the point", I see as "FUD". There's no reason for the FUD in this answer either; it would have been perfectly fine without it. `` is just older; from before C++ was standardized. The standard does not require it to be there, but if the compiler does provide it it's not being *contrary* to the standard either. And if the "whole point" is `using namespace std;`, then it's a bad point anyway -- using `using namespace std;` is a horrible suggestion. – Billy ONeal Mar 03 '11 at 06:52