C++ Issues with windows.h, Illegal declaration of anonymous 'struct' in vs17 - c++

When the following code is compiled I get these errors:
Error C2467 illegal declaration of anonymous 'struct'
C:\Program Files (x86)\Windows Kits\10\Include\10.0.18362.0\um\winnt.h 12723
Error C2133 '_IMAGE_POLICY_METADATA::Policies': unknown size
C:\Program Files (x86)\Windows Kits\10\Include\10.0.18362.0\um\winnt.h 20801
Error C2467 illegal declaration of anonymous 'struct'
C:\Program Files (x86)\Windows Kits\10\Include\10.0.18362.0\um\winioctl.h 4327
The code:
#include <iostream>
#include <chrono>
#include <thread>
#include <windows.h>
using namespace std;
int main()
{
std::cout << "Timer!\n Enter a number of seconds: \n";
int n;
std::cin >> n;
std::this_thread::sleep_for(std::chrono::milliseconds(n*1000));
std::cout << "Timer is up";
std::cout << '\a';
return 0;
}
These errors do not occur when windows.h is removed, as I am somewhat new I could be making a basic mistake however a lot of tutorials use it and it simply does not want to work. I have used a very basic snippet of code so that it is easier to determine whether it is a mistake on my behalf or an error somewhere else.
Windows 10, Visual Studio 2019 16.2.5

As #FrançoisAndrieux mentions in the comments, the windows.h header requires not enabling the "Disable Language Extensions" option under C/C++ -> Language (switch /Za).
However, if you are only looking to compile simple code that does not require windows.h, simply remove it. You can write:
#include <iostream>
#include <chrono>
#include <thread>
int main()
{
std::cout << "Timer!\n Enter a number of seconds: \n";
int n;
std::cin >> n;
std::this_thread::sleep_for(std::chrono::milliseconds(n*1000));
std::cout << "Timer is up\a";
return 0;
}

Both of the two error descriptions are related to that switch which is as #Acorn said. The link below is Microsoft's official document. If you can't solve your problem in this way. Maybe you need to consider using VS tool to repair your environment.
https://learn.microsoft.com/en-us/cpp/error-messages/compiler-errors-1/compiler-error-c2467?view=vs-2015
https://learn.microsoft.com/en-us/cpp/error-messages/compiler-errors-1/compiler-error-c2133?view=vs-2015

Related

MS Visual Studio C++: Won't use std namespace

I just recently made an app on Visual Studio, but now it's giving me this silly problem I can't make sense of. I've been doing this stuff in my other previously made apps and everything worked fine. I do it now and it seems to be giving me problems.
I tried starting a new app and this is what I get when I include I/O stream and use namespace std.
The code:
// ConsoleApplication1.cpp : Defines the entry point for the console application.
//
#include <iostream>
#include "stdafx.h"
using namespace std;
int main()
{
cout << "Hi" << endl;
return 0;
}
I really don't get what the problem is. Any help would be appreciated.
The errors:
'cout': undeclared identifier
'endl' : undeclared identifier
This problem is very easy to fix, all you need to do is move the
"stdafx.h" to the very top of your file.

((Code::Blocks)) - fatal error: stdafx.h: No such file or directory

I am using Code::Blocks version 16.01, and I am learning from www.learncpp.com, on lesson 1.3a. My code looks like this:
#include <iostream>
#include "stdafx.h"
int main()
{
std::cout << "Enter a number: "; // ask user for a number
int x = 0;
std::cin >> x; // read number from console and store it in x
std::cout << "You entered " << x << std::endl;
return 0;
}
The entire program works fine, but there's only one error and that's the #include stdafx.h; I'm getting the error listed in the title. Help and a somewhat clear explanation would be nice for me (I'm clearly inexperienced).
stdafx.h is used by Visual Studio's implementation of precompiled headers. If you aren't using VS either
Copy the stdafx.h file from your Visual Studio project folder into the Code::Blocks project folder and carry on with the build.
or
If you don't have a Visual Studio project, delete the #include "stdafx.h" liine. Odds are pretty good that if you don't have stdafx.h there is nothing in it that you need. The compiler well let you know pretty quickly if the odds weren't in your favour.
You delete #include "stdafx.h"
#include "stdafx.h"
We use in visual studio :).

Is <cmath> or <math.h> really needed? Compiles without it

So, I have the following code, and it builds and runs perfectly, tried various values and all is well. You'll notice I use log10 function and I do not include cmath or math.h. Why does it still build and run fine? Are those libraries really needed? Why/Why not? Does it have anything to do with me using visual studio? Like, would it not compile if say I was using a different IDE or command prompt to compile it?
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
cout << "Classify solutions as acidic or nonacidic" << endl<< endl;
//declaring double molar concentration
double mc = 1;
//using while and if statements to calculate pH in fixed notaion and acidic or nonacidic
while (mc != 0)
{
cout << "Please enter a molar concentration (or enter 0 to exit): ";
cin >> mc;
if (mc != 0)
{
cout << "Molar Conentration = " << scientific << mc << endl; //scientific notation
double pH = -log10(mc);
cout << "pH = " << fixed << setprecision(6) << pH << endl; //6 deciumals
if (pH > 7)
{
cout << "Nonacidic" << endl << endl;
}
else if (pH < 7)
{
cout << "Acidic" << endl << endl;
}
else
cout << "Neutral" << endl << endl;
}
}
//end program when inputing 0
cout << "End of Program" << endl;
return 0;
}
The code:
i = i++ + ++i;
may also compile okay, but that doesn't make it a good idea :-)
It would be wise to include headers for library functions that you use. You don't lose any functionality by doing so but you do guarantee the functionality will work (misuse notwithstanding).
Detailed analysis follows.
Even if an implementation is relaxed about this, the standard mandates it. C++11 17.6.2.2 Headers /3 states:
A translation unit shall include a header only outside of any external declaration or definition, and shall include the header lexically before the first reference in that translation unit to any of the entities declared in that header.
The gcc compiler, for example, will grumble bitterly about your code:
xyzzy.cpp: In function 'int main()':
xyzzy.cpp:22:34: error: 'log10' was not declared in this scope
double pH = -log10(mc);
^
As to why VC++ seemingly violates this rule, it has to do with the fact that header files are allowed to include other header files.
If you compile your code to produce pre-processor output (with /P), you'll find a line buried deep within it thus (at least in VS2013):
#line 1 "c:\\blah\\blah\\vc\\include\\cmath"
And a bit of analysis turns up the following hierarchy of includes:
iostream
istream
ostream
ios
xlocnum
cmath
(<xlocnum>, one of the internal headers used by <locale>, appears to need ldexp() from the <cmath> library, though there may be others as well).
That's further evidenced by the fact that VC++ does complain about the following code:
//#include <iostream>
using namespace std;
int main() {
double oneHundred = 100;
int two = log10 (oneHundred);
return two;
}
with:
error C3861: 'log10': identifier not found
but that error disappears the instant you uncomment the iostream inclusion line.
However, as previously stated, that is not behaviour you should rely on. If you are going to use a library function (or macro/template/whatever), it's up to you to include the correct header.
Otherwise your program compiling correctly is simply an accident.
As you've noticed, the code snippet you provided works in Visual Studio but not with other compilers. This is because of how the standard library is implemented for each compiler.
It turns out that when you include Visual Studio's implementation of <iostream>, you end up including a bunch of other headers indirectly, and one of these headers is <cmath>.
To see the exact chain, navigate to the standard library include directory. For me (I use Visual Studio 2013 Community Edition) this is located at
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include
Open iostream. Note the line #include <istream>
Open istream. Note the line #include <ostream>
Open ostream. Note the line #include <ios>
Open ios. Note the line #include <xlocnum>
Open xlocnum. Note the line #include <cmath>
Guess what? You included cmath when you included iostream... so your code is good to go, on Visual Studio at least. But, don't rely on implementation details or your code will break if you try to migrate it to another platform/toolchain.
For example, trying to compile the provided snippet using g++ on Cygwin results in the following error:
temp.cpp: In function ‘int main()’:
temp.cpp:22:34: error: ‘log10’ was not declared in this scope
double pH = -log10(mc);
This must mean g++'s implementation of <iostream> doesn't depend on <cmath>

visual studio C++ header file

Visual Studio is not recognising my #include 'Header.h' file. I have created the file in the Header Files in solution explorer and also tried manually pointing to the file. What I don't understand is, until yesterday there was absolutely no problem. Therefore, a simple cout doesn't work.
#include 'Header.h';
int main()
{
cout << "hi";
return 0;
}
#include "Header.h"
#include <iostream>
using namespace std;
int main()
{
cout << "hi";
return 0;
}
Not that Header.h is used in anyway, this is still the correct syntax.
You need #include <iostream> to be able to use cout.
You've got syntax errors in your #include preprocessor directive. Replace single with double quotes and drop the semicolon:
#include "Header.h"
Found the problem : I had to go to properties bar and change "Included In Folder" value to true from false.

error C2065: 'cout' : undeclared identifier

I am working on the 'driver' part of my programing assignment and i keep getting this absurd error:
error C2065: 'cout' : undeclared identifier
I have even tried using the std::cout but I get another error that says:
IntelliSense: namespace "std" has no member "cout"
When I have declared using namespace std, included iostream and I even tried to use ostream
#include <iostream>
using namespace std;
int main () {
cout << "hey" << endl;
return 0;
}
I'm using Visual Studio 2010 and running Windows 7. All of the .h files have using namespace std and include iostream and ostream.
In Visual Studio you must #include "stdafx.h" and be the first include of the cpp file. For instance:
These will not work.
#include <iostream>
using namespace std;
int main () {
cout << "hey" << endl;
return 0;
}
#include <iostream>
#include "stdafx.h"
using namespace std;
int main () {
cout << "hey" << endl;
return 0;
}
This will do.
#include "stdafx.h"
#include <iostream>
using namespace std;
int main () {
cout << "hey" << endl;
return 0;
}
Here is a great answer on what the stdafx.h header does.
write this code, it works perfectly..
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
cout<<"Hello World!";
return 0;
}
I had same problem on Visual Studio C++ 2010. It's easy to fix. Above the main() function just replace the standard include lines with this below but with the pound symbol in front of the includes.
# include "stdafx.h"
# include <iostream>
using namespace std;
The include "stdafx.h" is ok
But you can't use cout unless you have included using namespace std
If you have not included namespace std you have to write std::cout instead of simple cout
If the only file you include is iostream and it still says undefined, then maybe iostream doesn't contain what it's supposed to. Is it possible that you have an empty file coincidentally named "iostream" in your project?
I have seen that if you use
#include <iostream.h>
then you will get the problem.
If you use
#include <iostream>
(notice - without the .h)
then you will not get the problem you mentioned.
If you started a project requiring the #include "stdafx.h" line, put it first.
I've seen similar things happen when I was using the .c file extension with C++ code. Other than that, I'd have to agree with everyone about a buggy installation. Does it work if you try to compile the project with an earlier release of VS? Try VC++ Express 2008. Its free on msdn.
Such a silly solution in my case:
// Example a
#include <iostream>
#include "stdafx.h"
The above was odered as per example a, when I changed it to resemble example b below...
// Example b
#include "stdafx.h"
#include <iostream>
My code compiled like a charm. Try it, guaranteed to work.
The code below compiles and runs properly for me using gcc. Try copy/pasting this and see if it works.
#include <iostream>
using namespace std;
int bob (int a) { cout << "hey" << endl; return 0; };
int main () {
int a = 1;
bob(a);
return 0;
}
I have VS2010, Beta 1 and Beta 2 (one on my work machine and one at home), and I've used std plenty without issues. Try typing:
std::
And see if Intellisense gives you anything. If it gives you the usual stuff (abort, abs, acos, etc.), except for cout, well then, that is quite a puzzler. Definitely look into your C++ headers in that case.
Beyond that, I would just add to make sure you're running a regular, empty project (not CLR, where Intellisense is crippled), and that you've actually attempted to build the project at least once. As I mentioned in a comment, VS2010 parses files once you've added an include; it could be that something stuck the parser and it didn't "find" cout right away. (In which case, try restarting VS maybe?)
Take the code
#include <iostream>
using namespace std;
out of your .cpp file, create a header file and put this in the .h file. Then add
#include "whatever your header file is named.h"
at the top of your .cpp code. Then run it again.
I had the same issue when starting a ms c++ 2010 project from scratch - I removed all of the header files generated by ms and but used:
#include "stdafx.h"
#include <iostream>
using namespace std;
int main() {
cout << "hey" << endl;
return 0;
}
I had to include stdafx.h as it caused an error not having it in.
Try it, it will work. I checked it in Windows XP, Visual Studio 2010 Express.
#include "stdafx.h"
#include <iostream>
using namespace std;
void main( )
{
int i = 0;
cout << "Enter a number: ";
cin >> i;
}
before you begin this program get rid of all the code and do a simple hello world inside of main. Only include iostream and using namespace std;.
Little by little add to it to find your issue.
cout << "hi" << endl;
Are you sure it's compiling as C++? Check your file name (it should end in .cpp). Check your project settings.
There's simply nothing wrong with your program, and cout is in namespace std. Your installation of VS 2010 Beta 2 is defective, and I don't think it's just your installation.
I don't think VS 2010 is ready for C++ yet. The standard "Hello, World" program didn't work on Beta 1. I just tried creating a test Win32 console application, and the generated test.cpp file didn't have a main() function.
I've got a really, really bad feeling about VS 2010.
When you created your project, you did not set 'use precompiled headers' correctly. Change it in properties->C/C++->precompiled headers.
In Visual studio use all your header filer below "stdafx.h".
Just use printf!
Include stdio.h in your stdafx.h header file for printf.
Include the std library by inserting the following line at the top of your code:
using namespace std;
is normally stored in the C:\Program Files\Microsoft Visual Studio 8\VC\include folder. First check if it is still there. Then choose Tools + Options, Projects and Solutions, VC++ Directories, choose "Include files" in the "Show Directories for" combobox and double-check that $(VCInstallDir)include is on top of the list.
I ran across this error after just having installed vs 2010 and just trying to get a nearly identical program to work.
I've done vanilla C coding on unix-style boxes before, decided I'd play with this a bit myself.
The first program I tried was:
#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[])
{
cout << "Hello World!";
return 0;
}
The big thing to notice here... if you've EVER done any C coding,
int _tmain(int argc, _TCHAR* argv[])
Looks weird. it should be:
int main( int argc, char ** argv )
In my case I just changed the program to:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello world from VS 2010!\n";
return 0;
}
And it worked fine.
Note: Use CTRL + F5 so that the console window sticks around so you can see the results.
I came here because I had the same problem, but when I did #include "stdafx.h" it said it did not find that file.
What did the trick for me was: #include <algorithm>.
I use Microsoft Visual Studio 2008.
These are the things that you can use then, incl. 'count': Link
Had this problem, when header files declared "using namespace std;", seems to be confusing for GNU compiler;
anyway is bad style!
Solution was providing std::cout ... in headers and moving "using namespace std" to the implementation file.
In VS2017, stdafx.h seems to be replaced by pch.h see this article,
so use:
#include "pch.h"
#include <iostream>
using namespace std;
int main() {
cout << "Enter 2 numbers:" << endl;
It was the compiler - I'm now using Eclipse Galileo and the program works like a wonder