Cursor movement - COORD is not defined for some reason

I have included windows.h. I used the following code:
1
2
3
4
COORD coord;
coord.X = x; // your X cord
coord.Y = y; //your Y cord
SetConsoleCursorPosition(GetStdHandle( STD_OUTPUT_HANDLE ),coord);

And I get the following error:

`COORD' undeclared (first use this function)

I was informed that this was the correct code to use by this site (probably irrelevant, but if that's an unreliable site...):

http://www.experts-exchange.com/Programming/Languages/.NET/Visual_CPP/Q_23884165.html

Also, the code looked familiar. I think I've used it before (and it worked), I just couldn't remember what it was. What am I doing wrong?
I wrote a quick program in VS and included "Windows.h" and it worked fine:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include "windows.h"

using namespace std;

int main()
{
	int x = 2;
	int y = 2;

	cout << "line0" << endl;
	cout << "line1" << endl;
	cout << "line2" << endl;
	cout << "line3" << endl;

	COORD coord;
	coord.X = x; // your X cord
	coord.Y = y; //your Y cord
	SetConsoleCursorPosition(GetStdHandle( STD_OUTPUT_HANDLE ),coord);
}


So you're saying that your code doesn't compile? It sounds like it doesn't know about the definition of the COORD struct in "WinCon.h" (which is included by "Windows.h"). Can you try this code out and see if it works?

That works for me. I tried something else: putting #include "windows.h" in the file that needed it fixed the problem. Why would this work but not including it from another file (windows.h is the first file that is included)?
Because in a project, .c/.cpp files don't share includes.
Topic archived. No new replies allowed.