C++ <iostream.h> Error - c++

I am at the absolutely newest level of new when it comes to C++. It may seem like a noob mistake, but I think I'm missing something with my first program, "Hello World!".
I'm running from Ubuntu (not sure if this is any different from working with Windows), and I'm using a book called Teach Yourself C++ in 21 Days.
The code I'm resembling looks exactly like this:
#include <iostream.h>
int main()
{
cout <<"Hello World!\n";
return 0;
}
I have this exactly in my text editor, but I keep getting greeted by the same error whenever I try to compile it!
first.cpp:2:22: fatal error: iostream.h: No such file or directory
compilation terminated.
I'm pretty distressed as this is literally the first step in my coding career! I'm not sure if ubuntu needs to be treated differently than Windows (which is what the book is using as reference).
Help!

There are two problems here:
You need to omit the .h suffix:
#include <iostream>
Also, cout is an unqualified name, and needs to be qualified with the std namespace since you are not using namespace std:
std::cout << "Hello World!\n";

There shouldn't be any iostream.h it's simply called iostream and should look like this:
#include <iostream>
int main()
{
std::cout <<"Hello World!\n";
return 0;
}
(also notice the std:: before the cout, since it means that it's from the standard namespace.)

You want just iostream
#include <iostream>
I suspect the book is very old. Names are qualified in the std namespace, so you may want to add
using namespace std;
For now at least.

use
#include <iostream>
in general STL header file don't have a .h

you need to compile using g++ compiler, not by gcc
g++ hello.cpp

Related

#include<iostream.h> works well in turboc++ but not in Visual studio. why?

I'm using turboc++ 4.0 and Visual studio 2013. I just started learning programming. when I write the code.
#include<iostream.h>
#include<conio.h>
int main()
{
cout<<"hello!";
getch();
return 0;
}
it works well in turbo, but visual stdio shows an error
fatal error C1083: Cannot open include file: 'iostream.h': No such file or directory.
and when I use
using namespace std;
it shows another error about using getch();.
Does every compiler have its own syntax?
"Does every compiler have it's own syntax?"
No, there's a standard every compiler needs to implement.
Turbo-C++ was made before any standards were established and is merely the only compiler still around, that doesn't implement them.
The standard compliant way to write your program looks like:
#include <iostream>
int main() {
std::cout<<"hello!" << std::endl;
char dummy;
std::cin >> dummy;
}
Note: You shouldn't use using namespace std;, but explicitly put the std:: scope when needed, or use using std::cout cout;, etc.
Turbo C++ is from the middle or early 1990's, before the standardization of C++.
At that time the effective standard for C++ was the ARM, the Annotated Reference Manual by Bjarne Stroustrup and Margaret Ellis (IIRC), and it used <iostream.h>.
With the first standardization in 1998 <iostream.h> was dropped, and replaced with just <iostream>. The standard header places cin and cout in namespace std, so you can't just change the header name. It's not guaranteed, but you may possibly be able to make your code work by writing
#include <iostream>
using namespace std;

C++ cout gives undeclared identifier

So, I have this question. Why does cout throws
error C2065: 'cout' : undeclared identifier
I am using Visual Studio 2012 as an IDE and I am writing a school project. I have everything done except an example file. So I am trying to write something on the screen like this:
#include "iostream"
#include "stdafx.h"
using namespace std;
int main()
{
cout<<"example";
return 0;
}
So the problem is with cout... printf works fine, but I want to use cout.
EDIT:
I've changed "" to <> but it is not helping. Also I am using this code only for example... This is not the whole project.
stdafx.h shall be the first include directive in your source file.
Switch files and convert the second include to <>, as other suggested.
#include "stdafx.h"
#include <iostream>
See this post for more information.
First of all:
#include <iostream>
instead of #include "iostream"
Secondly, it is generally considered bad practice to write using namespace std;, even though most courses start with that. It is better to only use what you actually need, in your case:
using std::cout;
#include "iostream"
should be
#include <iostream>
Quoting from this post:difference-between-iostream-and-iostream-quotes-in-include
By courtesy of #Jerry Coffin's answer:
When you use < >, the compiler only looks in the system-designated directory/directories (e.g., whatever you've set in the include environment variable) for the header.
When you use " ", the compiler looks in the local directory first, and if that fails, re-searches just like you'd used < >. Technically, (i.e., according to the standard) that doesn't have to be the "local" directory, but that's how it works in essentially every compiler of which I'm aware).
EDIT:
However, the root cause is that stdafx.h is a precompiled header. Visual C++ will not compile anything before the #include "stdafx.h" in the source file, unless the compile option /Yu'stdafx.h' is unchecked (by default); it assumes all code in the source up to and including that line is already compiled. However, it is still better to use <> with iostream not to confuse reader of the code.
If you use #include <iostream> with the <> instead of "" then it should work. Right now, the compiler doesn't know where to find the iostream library.
Also, you might want to change cout<<"example"; to cout<<"example"<<endl; for a new line so that it formats correctly.
Came across this issue while trying to build a Dynamic Linked Library. Make sure that instead of the #include stdafx.h you specify the following include on the first line of your .cpp file:
#include "pch.h"
This should also be the case for VS2017 or earlier.
This error also occurred in the Visual Studio 2017 IDE. Moving stdafx.h to the top solved the error.
For more on stdafx.h, see What's the use for "stdafx.h" in Visual Studio?

c++ simple program error

I have created a file called untitled1.cpp in dev-cpp with the following script:
#include <iostream.h>
using namespace std;
int main(){
cout << "C++";
return 0;
}
But the compiler shows errors like:
1 F:\Dev-Cpp\include\c++\3.4.2\backward\iostream.h:31,
from F:\Dev-Cpp\Untitled1.cpp In file included from
include/c++/3.4.2/backward/iostream.h:31, from
F:\Dev-Cpp\Untitled1.cpp 32:2
F:\Dev-Cpp\include\c++\3.4.2\backward\backward_warning.h #warning This
file includes at least one deprecated or antiquated header. Please
consider using one of the 32 headers found in section 17.4.1.2 of the
C++ standard. Examples include substituting the header for the
header for C++ includes, or instead of the deprecated
header . To disable this warning use -Wno-deprecated.
What is the error that I have? How do I fix it?
In C++ you import the standard library without using the .h suffix.
#include <iostream>
So your fixed example:
#include <iostream>
int main(int argc, char **argv) {
std::cout << "C++";
return 0;
}
Your code is not standard C++. You should say #include <iostream> (no ".h"!). Whatever source you have been learning this from is about 25 years out of date, and you should consider getting some more modern material.
(The "iostreams.h" header was part of a very early non-standard library in the early 1990s, and so it's being kept around for "compatibility" reasons, or to catch very inert programmers and give them a helpful hint.)
Use header file as #include<iostream> instead of #include<iostream.h>
Include iostream instead of iostream.h
This is just a warning.
I think that you could try to include iostream instead of iostream.h in order to fix it.
It says that the header, in this case, iostream.h is deprecated or antiquated. (You only have one header, so that's the one! Just read the error message!)
So you'll have to use iostream, not iostream.h.
You've posted the reason in your question already!
This file includes at least one deprecated or antiquated header.
The real question should therefore be: "Which one is antiquated, how do I replace it?", not "What's the error". Answer: Use <iostream>. The <*.h> versions are pre-standard, legacy headers.
So: Read error messages, people.

G++ not finding <iostream.h> in Ubuntu

I just installed Ubuntu and tried making the famed "Hello World" program to make sure that all the basics were working. For some reason though, g++ fails to compile my program with the error: "'cout' is not a member of 'std'". I've installed the build-essential package. Am I missing something else?
#include <iostream.h>
int main() {
std::cout << "Hello World!" << std::endl;
return 0;
}
Looks pretty good to me...
Use #include <iostream> - iostream.h is not standard and may differ from the standard behaviour.
See e.g. the C++ FAQ lite entry on the matter.
The standard header is called <iostream>, not <iostream.h>. Also, it is agood idea to compile your C++code with the -Wall and -pedantic flags, which can point out lots of errors with non-standard code that g++ would otherwise ignore. Use:
g++ -Wall -pedantic myprog.cpp
Sounds like it did find iostream.h but it does not define cout in the std namespace. It is there for backwards compatibility with older programs that expect cout to be in the global namespace.
use
#include<iostream>
using namespace std;
without namespace you won't be able to use cout or cin

Yet another linker issue

I'm having a linking issue with a basic C++ program. No, I'm not including .cpp files!
This is what's happening.
main.cpp:
#include "header.h"
#include <iostream>
int main() {
std::cout << "Hello!";
}
header.h:
#ifndef _HEADER_H
#define _HEADER_H
class Something {
public:
printContents();
};
#endif
something.cpp:
#include "header.h"
#include <iostream>
Something::printContents() {
cout << "This class's Contents!!";
}
What's happening is that I get a compiler error going: multiple definitions of some standard C function, such as strtod:
g++ -o ... main.o
build/....main.o: In function
`strtod':
../MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/stdlib.h:318:
multiple definition of `strtod'
build/..something.o:...something.cpp:(.text+0x0):
first defined here collect2: ld
returned 1 exit status
If I get rid of #include <iostream> in one of the two occasions and get rid of the couts, it will compile. What's going on? I'm using g++ and NetBeans to compile.
I tried in the command line:
g++ *.h *.cpp -o program
and the same thing happened.
Please note that _HEADER_H is an illegal name in C++ user code - names beginning with the underscore and an uppercase letter are reserved for the C++ implementation. This does not normally cause noticeable problems, but when you use what may be a common name in the implementation like HEADER in this context, it well might.
Modify,
Something::printContents()
{
std::cout << "This class's Contents!!";
}
NOTE: Specify the return datatype.
One of your problems is right here:
I tried in the command line: g++ *.h
*.cpp -o program
Don't pass your header files... Try something like this:
g++ *.cpp -o program
I could not reproduce your exact problem. I get this to compile and link nicely with the following few notes:
Add a void return type to the printContents-function (So it says void printContents(); in the header and void Something::printContents() { in the implementation-file)
Use std::cout rather than just cout. cout is not defined in the scope it is used
Make sure header.h ends with a blank line
Use HEADER_H rather than _HEADER_H (like Neil Butterworth says)
I use the command line g++ main.cpp something.cpp to compile.
I see a couple of problems. You shuold define the returning value of the function
printContents()
and you must write
std::cout
if you don't write
using namespace std;
The problem was in a multi-installation of MinGW. I had one already installed, and when I got Qt on my computer, it had installed it's own MinGW. Bummer, I ported the code to my university's servers and it ran OK.
Bummer!!
Thanks everybody for the help, I will definitely follow your guidelines in the future.
Header names - no underscores
Correct return type
Real code in the forums!
Leo Bruzzaniti