2

I am using Visual Studio 2019, and my code uses console outputs which change colors frequently. I am including Windows.h in my code, which is the header file that contains SetConsoleTextAttributes, whereas STD_OUTPUT_HANDLE should be initialized by using namespace std. My code in its entirety can be found here, but the following is the section with the error:

#include <iostream>
#include <cmath>
#include "HeadFile.h"
#include <windows.h>
#include <string.h>

using namespace std;

int Play(char(&spaces)[7][6], int(&color)[7][6], int player, int playerOneWins, int playerTwoWins, int ties)
{
    HANDLE hConsole;
    hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
.....

The code runs fine, but inside of studio itself, I see the following error:

enter image description here

The error is coming from the first instance of STD_OUTPUT_HANDLE only (another case at the bottom of the picture has no errors). If I comment out the first one, the next instance errors:

enter image description here

How can I fix this issue? I've read in a few non-related posts that using namespace std can sometimes lead to problems. Is this the case?

David
  • 250
  • 4
  • 15
  • 1
    `STD_OUTPUT_HANDLE` is a preprocessor macro, it is not affected by `using namespace std;` (which [you should not be using to begin with](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice)) . What you describe is an IDE issue, not a coding issue. – Remy Lebeau Apr 04 '20 at 04:32
  • 2
    Code analysis tools have to balance accurate and fast, and sometimes fast wins and the tool spits out a false positive. Wish I had a solution for you, but instead all I can do is share the pain. I have rewritten valid code to eliminate bogus warning more than once. – user4581301 Apr 04 '20 at 04:37
  • @RemyLebeau Good to know, I'll see if there's anything I can do from VS – David Apr 04 '20 at 04:40
  • @user4581301 I'll keep that in mind, thanks. I've had that happen before in Eclipse but never VS – David Apr 04 '20 at 04:40
  • 1
    Don't get me started on Eclipse. Chokes on forward-declarations of nested classes, and I have a LOT of of those suckers serving up PIMPLs. – user4581301 Apr 04 '20 at 04:50

3 Answers3

3

Use Header file "Windows.h" instead of "windows.h".

  • So you think that capitalisation of file names is the problem in a windows environment. Please elaborate. – Yunnosch May 30 '20 at 08:55
  • 2
    Yes since the C++ and a lot of other languages are case sensitive, the names need to be written exactly the same. – Mehroz Mustafa May 30 '20 at 09:06
  • Muneeb Elahi Malik thanks for your extremely concise answer! @MehrozMustafa It is good to know that C++ library names are case sensitive! – cmarangu May 17 '22 at 02:06
0

I had the same problem, adding #include <stdlib.h> fixed it for me.

e45g
  • 1
  • 1
0

I had the same problem, you need to use #include <Stdio.h>

Micsha
  • 1
  • There is no such standard GCC library named as Stdio.h. I think you are referring the one with small s? – Abhishek Dutt Dec 17 '22 at 12:39
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/33406064) – CinCout Dec 17 '22 at 12:51