Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 697
Toby Hogg Avatar asked Sep 20 '19 19:09

Toby Hogg


1 Answers

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;
}
like image 78
Acorn Avatar answered Sep 29 '22 13:09

Acorn