C++ cout gives undeclared identifier - c++

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?

Related

Why does changing the order of including psapi.h gives compilation erros?(Indentifier BOOL is undefined)

I am using Visual Studio Community 2017 to code c++. When I run the following code everything works fine.
#include "pch.h"
#include<Windows.h>
#include<Psapi.h>
#include <iostream>
#include <conio.h>
int main()
{
std::cout << "Really!! How do you do it?";
_getch();
}
But if I change the order of #includes by including psapi.h before Windows.h, compiler goes badass and throws 198 errors at me, which surprisingly(maybe only to me) includes Identifier "BOOL" is undefined.
Why is this happening?
Since Psapi.h's include tree is trivial, I'm going to exemplify. Everything relies on VStudio 2015 (Community) (v14.0.25431.01 Update 3) and Windows Kits 8.1 (? funny, because v10 is there too) files (with default env vars and preprocessor definitions):
BOOL is defined in minwindef.h (#157: typedef int BOOL;)
Psapi.h only includes one file (#27: #include <winapifamily.h>)
winapifamily.h doesn't include any other file
So, when reaching Psapi.h (#87: BOOL WINAPI EnumProcesses (...), the compiler doesn't know anything about BOOL, so it complains.
Windows.h includes minwindef.h (indirectly, via windef.h), and that's why it works when you include it before Psapi.h.
Personally, I think it's a bug in Psapi.h, since it's not self contained, but there might be a good reason (that I'm not aware of) for that. Anyway, if this is indeed a bug, it wouldn't be MS's 1st one :)
#include <Windows.h>
#include <WinSock2.h>
// main present just for rigorosity's sake
int main() {
return 0;
}
to answer the question, I know this is DATED but the issues persist today. You need the following:
#include "stdafx.h"
#include <stdbool.h>
#include <Windows.h>
#include <stdlib.h>
#include <psapi.h>
After stdlib.h was included, the errors were gone.

There are unresolved includes inside <iostream>

I just tried to compile my C++ code and an error appears when I try to do so.
The error appears on line 9
Here are the versions of the gcc and g++ and such
Any help would be appreciated.
Edit:
I am also including Movie.h:
And also Movie.cpp:
https://puu.sh/vb53G/9e9abd1832.png (I was not able to include more than 3 images due to restrictions)
Firstly, in your Movie.h file, you have not included the string header file correctly. It should be:
#include <string> // without the .h extension
error: 'string' does not name a type
Secondly, you have forgotten to add the closing parenthesis of the constructor function of class "Movie". I am assuming that you have added this now, after the edit
As for the marking done by your compiler, you may find the following StackOverflow post helpful:
StackOverflow Post: Unresolved inclusion iostream.
The link is for the Eclipse IDE, but you can find a similar solution for your own IDE (I cannot tell which one you have).
The line under the #include is just a warning (I'm not sure why).
However, the errors are from the "Movie" class:
1. add "using namespace std" on the top of this class.
2. close the parenthesis on the constructor of 'Movie'.
The error messages are fairly clear:
'string' does not name a type
That is, the compiler is unaware of the type string because either:
you have not #include <string> in Movie.h
or you have, but have not brought it into your namespace with a using namespace std;
although why not just refer to it as std::string?
You are missing
#include <string>

cin and cout operator error C++

I am doing an assignment for school and at school we use codeblocks for our IDE, but I wanted to use visual studio at home. The problem is when I run my program on visual studio I continue to get an operator error on my cin and cout but only before a string. I tried #include and while the error lines went away when I build I get the same error before. I am new to C++ and rather confused any help is appreciated.
picture of code below
The #include <string> you tried would be correct.
Your main problem is the #include "stdafx.h". It's a Precompiled header. Visual studio puts it in by default although it isn't required. It has to be the first include if you use it:
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.
Just put it as the first #include (or get rid of it completely) to fix your issue. Then the rest should start working normally.

'printf': identifier not found

I have included stdio.h into my C++ project, why am I still getting this error? Also, after I added #include , printf(), in my code, was no longer underlined in red to suggest that there was any error.
Also, I would like to use the function, format(). Which library is that found in?
you must include stdio.h instead of cstdio.h
#include <stdio.h>
Use #include< cstdio>
using namespace std;
after that you can use printf()

Boost build error with websocketpp and MySQL on Windows

I am trying to build a C++ app that uses both websocketpp and MySQL. I have encountered 2 build problems using VS 2010 C++ Express.
1) A problem with the boost libraries. It produces many errors like this:
1>c:\program files (x86)\boost\boost_1_50\boost\thread\win32\thread_data.hpp(210): error C2146: syntax error : missing ')' before identifier 'rel_time'
Here's the relevant snippet from thread_data.hpp starting with line 210:
inline BOOST_SYMBOL_VISIBLE void sleep(TimeDuration const& rel_time)
{
interruptible_wait(detail::pin_to_zero(rel_time.total_milliseconds()));
}
2) A conflict with the word VERSION which is documented here and I believe is independent.
To make a clear and simple example of the boost build problems, I'm using the websocketpp example: echo_server.cpp to which I added these includes:
#include "stdafx.h"
Boost lib includes recommended by "Building a program with websocketpp" on the websocketpp site.
#include <boost/regex.hpp>
#include <boost/random.hpp>
#include <boost/system/api_config.hpp>
#include <boost/system/config.hpp>
#include <boost/system/error_code.hpp>
#include <boost/system/system_error.hpp>
#include <boost/system/windows_error.hpp>
and the MySQL header includes. Adding these 2 boostincludes triggers the build errors. If I comment out these 2 includes, it builds without errors:
#include <my_global.h>
#include <mysql.h>
Any suggestions on how to deal with the boost problems?
I don't think this is the same build problem as this one, "Trying to build websocket++ with MinGW: last few linker errors — what could it be?"
Concerning the first error, check if there are any macros interfering with the code. Right-click and go to definition or #define the macro yourself at the beginning of the file and see where it gets redefined. In really hard cases, look at the preprocessor output via a compiler flag.
Concerning the rest, you don't provide any versions for Boost and MySQL. Then, there is my_global.h (or is that part of MySQL?) and stdafx.h, which are both under your control but you don't show them here. Also, try to reduce the issue to the smallest possible piece of code. In short, provide a reproducible example.