C2065 'cout': undeclared identifier [closed] - c++

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
So I'm a complete noob to C++ and I'm trying to make a simple "Hello world" program for an assignment. My code is below:
#include "stdafx.h"
#include <iostream>
using namespace std;
int main() {
cout<<"hello world!";
return 0;
}
For some reason, VS2017 is throwing an error at cout, saying it's undefined. I've done some reading on old posts about this and added in #include "stdafx.h to see if that would solve it as per old advice, but it continues to give me the error. Any ideas?
EDIT:
Again a complete noob but there are multiple version of stdafx.h that come up when I search for it, here's what looks like the "main" one:
// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently,
// but are changed infrequently
#pragma once
#ifndef STRICT
#define STRICT
#endif
#include "targetver.h"
[!if SERVICE_APP]
#define _ATL_FREE_THREADED
[!else]
#define _ATL_APARTMENT_THREADED
[!endif]
#define _ATL_NO_AUTOMATIC_NAMESPACE
#define _ATL_CSTRING_EXPLICIT_CONSTRUCTORS // some CString constructors will be explicit
[!if PREVIEW_HANDLER || THUMBNAIL_HANDLER || SEARCH_HANDLER]
#ifdef _MANAGED
#error File type handlers cannot be built as managed assemblies. Set the Common Language Runtime options to no CLR support in project properties.
#endif
#ifndef _UNICODE
#error File type handlers must be built Unicode. Set the Character Set option to Unicode in project properties.
#endif
#define SHARED_HANDLERS
[!endif]
[!if SUPPORT_MFC]
#include <afxwin.h>
#include <afxext.h>
#include <afxole.h>
#include <afxodlgs.h>
#include <afxrich.h>
#include <afxhtml.h>
#include <afxcview.h>
#include <afxwinappex.h>
#include <afxframewndex.h>
#include <afxmdiframewndex.h>
#ifndef _AFX_NO_OLE_SUPPORT
#include <afxdisp.h> // MFC Automation classes
#endif // _AFX_NO_OLE_SUPPORT
[!endif]
[!if SUPPORT_COMPLUS]
#include <comsvcs.h>
[!endif]
#define ATL_NO_ASSERT_ON_DESTROY_NONEXISTENT_WINDOW
#include "resource.h"
#include <atlbase.h>
#include <atlcom.h>
#include <atlctl.h>

error C2065: 'cout': undeclared identifier is a result of the absence of #include <iostream>. The first cause might be stdafx.h content. The one you provided, I'm not so sure how it is related to your main.cpp/project. Let's start from a fresh project: ...VS2017 IDE: Create new project, ConsoleApplication project-type, & replace the main() function with yours.
A VS2017 IDE (15.8.2) fresh ConsoleApplication project: ConsoleApplication1
// ConsoleApplication1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
using namespace std;
int main() {
cout << "hello world!";
return 0;
}
stdafx.h: (Generated by the IDE)
// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//
#pragma once
#include "targetver.h"
// TODO: reference additional headers your program requires here
stdafx.cpp: (Generated by the IDE)
// stdafx.cpp : source file that includes just the standard includes
// ConsoleApplication1.pch will be the pre-compiled header
// stdafx.obj will contain the pre-compiled type information
#include "stdafx.h"
// TODO: reference any additional headers you need in STDAFX.H
// and not in this file
targetver.h: (Generated by the IDE)
#pragma once
// Including SDKDDKVer.h defines the highest available Windows platform.
// If you wish to build your application for a previous Windows platform, include WinSDKVer.h and
// set the _WIN32_WINNT macro to the platform you wish to support before including SDKDDKVer.h.
#include <SDKDDKVer.h>
** This code runs perfectly. **
--
"there are multiple versions of stdafx.h that come up when I search for it" - What do you mean? In your project? On the internet? You can't just take one stdafx.h from the internet. stdafx.h content is tailored per project, not universal. What I provided above, for example, is the IDE default new ConsoleApplication project stdafx.h. You may add to the file according to your project needs.

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.

LNK4006 error in XLW 4.0.0.f0

XLW is an open sourced library that wrap C++ code into XLL add-in for Excel. A strange LNK4006 error is reported in a project i'm handling, reporting an XLW function is "already defined".
I'm using XLW version 4.0.0.f0, to simplify the situation, I break down it to a toy project, there are only three files --
StdAfx.cpp, the boilterplate file :
#include "stdafx.h"
StdAfx.h, boilterplate but mysterious (I never understood the content)
#if !defined(AFX_STDAFX_H__0D719468_50D1_11D2_9DAF_00207813663F__INCLUDED_)
#define AFX_STDAFX_H_0_D719468_50D1_11D2_9DAF_00207813663F__INCLUDED_
#if _MSC_VER >= 1000
#pragma once
#endif // _MSC_VER >= 1000
#define VC_EXTRALEAN // Exclude rarely-used stuff from Windows headers
#include <afxwin.h> // MFC core and standard components
#include <afxext.h> // MFC extensions
#ifndef _AFX_NO_AFXCMN_SUPPORT
#include <afxcmn.h> // MFC support for Windows Common Controls
#endif // _AFX_NO_AFXCMN_SUPPORT
//{{AFX_INSERT_LOCATION}}
// Microsoft Developer Studio will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_STDAFX_H__0D719468_50D1_11D2_9DAF_00207813663F__INCLUDED_)
and then the puzzle, WhyLNK4006.cpp:
#include <xlw/xlw.h>
void WhyLNK4006()
{
xlw::XlfExcel &x = xlw::XlfExcel::Instance();
xlw::XlfExcel::Instance().FreeMemory();
}
This will trigger an LNK4006 error, which I could suppress it as a warning but couldn't guess why this happen:
xlw-vc120-mt-gd-4_0_0f0.lib(XlfExcel.obj) : warning LNK4006: "public:
void __thiscall xlw::XlfExcel::FreeMemory(bool)"
(?FreeMemory#XlfExcel#xlw##QAEX_N#Z) already defined in B15.obj;
second definition ignored
I couldn't understand, why there's such an error?
Both XLW and my toy project are compiled as /MTd, all XLW header file XXX.h have preprocessor protection e.g.
> #if !defined(XXX_H)
> #define XXX_H //the read content of XXX.h
> #endif
How come this could lead to a double definition?

'GetProcessIdOfThread': identifier not found

Here is my codes in stdafx.h :
// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//
#pragma once
#define _WIN32_WINNT 0x0502
#include "winsock2.h"
#include "windows.h"
#include "stdio.h"
#include "Iphlpapi.h"
#include <psapi.h>
#include "Ntsecapi.h"
#include "txdtc.h"
#include "xolehlp.h"
#include <iostream>
#include <tchar.h>
// TODO: reference additional headers your program requires here
As you see i have included "windows.h"
And here is main codes :
#include "stdafx.h"
...
if (hThread && dwRpcssPid == GetProcessIdOfThread(hThread))
...
My errors are :
'GetProcessIdOfThread': identifier not found
IntelliSense: identifier "GetProcessIdOfThread" is undefined
How can i fix these errors?
The function is not available with _WIN32_WINNT values less than 0x0600 AKA _WIN32_WINNT_VISTA. If you change your code this way, you will get it working:
//#define _WIN32_WINNT 0x0502
#define _WIN32_WINNT 0x0600
The function is available since Vista, to target Vista+ you should have this value defined respectively.
To target latest versions of API with current SDK, you can simply include SDKDDKVer.h and those values will be defined for you/
//#define _WIN32_WINNT 0x0502
#include <SDKDDKVer.h>
See also:
What is _WIN32_WINNT and how does it work?
GetProcessIdOfThread's platform requirements states:
Windows Vista [desktop apps only]
Windows Server 2003 [desktop apps only]
And the header requirements states:
Processthreadsapi.h on Windows 8 and Windows Server 2012
So:
Make sure your windows SDK is up-to-date
Make sure you have specified your platform requirements properly.
Make sure you're including the right header file.
If you are using windows 8 you need to include : Processthreadsapi.h
See the MSDN references in the header section.

Define from include breaking other previous includes

I'm trying to debug the includes of my project's main file. Here's my include code.
//Gameplay
#include "gameplay.h"
//LibNoise
#include <noise/noise.h>
//Console Window
#ifndef _WINDOWS_
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#undef KEY_EVENT
#undef MOUSE_EVENT
#endif
#include <io.h>
#include <fcntl.h>
//RakNet
#include "MessageIdentifiers.h"
#include "RakPeerInterface.h"
#include "BitStream.h"
#include "RakNetTypes.h"
//My Includes
#include "Island.h"
The problem is, gameplay.h includes a file (specifically ScriptController.h) where there's an enum that contains the words KEY_EVENT and MOUSE_EVENT which is included through some includes in windows.h (specifically wincon.h). This breaks the enum and I get errors during compilation. Note, it is actually including windows.h because _WINDOWS_ isn't defined at this point according to MSVS (so it's not like it's defined before gameplay.h or something).
I can't see why this would be a problem as gameplay.h is included before windows.h which should mean that I would get no trouble with replacement of the terms in the enum? Undefining them doesn't help either.
Where did I go wrong? Is there any way I can "debug" the preprocessor and see the output from the preprocessor that is causing this syntax error and some kind of #include chain? I want to be able to fix this myself next time if it occurs.
Here's the errors
Error 3 error C2065: 'CALLBACK_COUNT' : undeclared identifier c:\users\pf\downloads\gameplay-master\gameplay\src\scriptcontroller.h 1024 1 testerino2
Error 1 error C2059: syntax error : 'constant' c:\users\pf\downloads\gameplay- master\gameplay\src\scriptcontroller.h 769 1 testerino2
Error 2 error C3805: 'constant': unexpected token, expected either '}' or a ',' c:\users\pf\downloads\gameplay-master\gameplay\src\scriptcontroller.h 769 1 testerino2
Here's the wincon.h defines
#define KEY_EVENT 0x0001 // Event contains key event record
#define MOUSE_EVENT 0x0002 // Event contains mouse event record
Here's the offending code lines of ScriptController.h
762| enum ScriptCallback
763| {
764| INITIALIZE = 0,
...
768| RESIZE_EVENT,
769| KEY_EVENT,
770| MOUSE_EVENT,
771| TOUCH_EVENT,
...
775| GAMEPAD_EVENT,
776| CALLBACK_COUNT,
777| INVALID_CALLBACK = CALLBACK_COUNT
778| };
...
1024| std::vector<std::string> _callbacks[CALLBACK_COUNT];
To avoid clashes with windows.h defines, I'd recommend making your own header file that:
Defines any macros that affect windows.h, such as WIN32_LEAN_AND_MEAN
Does #include <winsock2.h> if used, and #include <windows.h>
Does #undef on any annoying macros
Then make sure every source file that might come across a Windows include, includes this header. You could perhaps use a compiler feature to inject the header into all units.

Syntax errors in C++ include file

I'm writing a game in c++ in microsoft visual studio 2010, yesterday I wrote a pong game and everything was fine but now the compiler telling me that there is a lot of errors for example:
1>w:\c++\planet escape\planet escape\room.h(25): error C2061: syntax error : identifier 'WorldMap'
And here is the Room.h file:
#pragma once
#include <allegro5/allegro.h>
#include <vector>
#include "Entity.h"
#include "WorldMap.h"
#include "Link.h"
#define ROOM_W 20
#define ROOM_H 20
class Room{
private:...
public:...
};
When in code there is no mistakes and it sees all the classes fine.
So what can cause such mistake?
EDIT:
here is the WorldMap.h
#pragma once
#include <allegro5/allegro.h>
#include "Room.h"
#include "Player.h"
#define WORLD_W 10
#define WORLD_H 10
class WorldMap{
private:...
public:...
};
If when I'm runing it he cant see it then why he see it when coding?
You have circular includes. Suppose you are compiling a file that has a #include "WorldMap.h" as the first applicable #include statement. The file WorldMap.h has that #include "Room.h" that is going to cause a lot of trouble. The problems start in earnest in Room.h at the #include "WorldMap.h" statement. That #include has no effect thanks to the #pragma once in WorldMap.h. When the compiler gets to the point of processing the main body of Room.h, the class WorldMap is neither defined nor declared.
Addendum
The solution is to get rid of those extraneous #include statements. The file WorldMap.h does not need to #include either of Room.h or Player.h. It instead needs to make forward declarations of the classes Room and Player. Similarly, you don't need all those #include statements in Room.h either.
It is in general a good idea to use forward declarations of types in your headers instead of including the file that defines the types. If the code in the header does not need to know details of the type in question, just use a forward declaration. Do not #include the header that defines the type.