You are here:

C++/GetMousePos

Advertisement


Question
Hello Ralph,

I was using the following code:

#include <iostream>
#include <windows.h>
using namespace std;

struct point {int x; int y;};

int getpos{
point pos;
GetMousePos(&pos);
return (pos);
}


int main(){
  getpos;
  cout << pos.x << '\n'<< pos.y;   
  return 0;
}



to see whether I could get the mouse pos, however, it does not seem to work.
I might be overlooking something stupid, as usual, but I've looked this over and over and I don't see any mistakes/errors...

I get errors like this:
'getpos' : looks like a function definition, but there is no parameter list; skipping apparent body

and surprizingly this aswell:
left of '.x' must have class/struct/union
       type is ''unknown-type''

I did "struct point" so this is kinda odd..


So the question is: How do we use GetMousePos?


thanks in advance

Answer
Well if you read what the error says and look at what you actually wrote and not what you thought you wrote you could find the error. You got the function definition form correct for main() so I shall assume you know how to define functions in C and C++.

You thought you wrote:

       int getpos() { ...

You actually wrote:

       int getpos{ ...

Can you spot the difference?

Here's a clue:

'getpos' : looks like a function definition, but there is no parameter list

The parameter list is the part in ( and ), which is missing in the code you sent me, i.e. "there is no parameter list" and in C and C++ it is _not_ optional when defining or declaring a function. As I said, take note of what the error message is saying and read what you wrote around the area it is complaining about!

Sorry if the above seems a little rude and belabouring the point but as you asked me rather than fixed the problem yourself I have to assume you do not know how you should handle such errors.

There are several other points about your code:

- getpos is declared as returning an int but you return a point struct.

- there is no Win32 function called GetMousePos (I assume Win32 as you include windows.h). I think you mean GetCursorPos.

- the standard Win32 point type is the POINT struct, a pointer to which is accepted by GetCursorPos.

So what do you need to do?

- fix the syntax for the definition of the getpos function.
- remove your point type definition.
- replace uses of your type point with the Win32 API type POINT.
- replace the call to GetMousePos with GetCursorPos.
- decide what you want to return from getpos: the x coordinate, the y coordinate or both and change getpos definition to suit.

Hope this helps and please, please, please do not use me to spot typos in your code (I have enough of my own <g>). And lookup the Win32 SDK functions and types you wish to use in the Microsoft developer documentation - the MS Developer Network Library which if you do not have a copy to hand can be found online at http://msdn.microsoft.com/ specifically at http://msdn.microsoft.com/library/. Finally you might wish to get yourself a good book on Windows programming such as "Programming Windows" by Charles Petzold.  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Ralph McArdell

Expertise

I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.

Experience

My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at http://www.open-std.org/jtc1/sc22/wg21/.

Education/Credentials

©2016 About.com. All rights reserved.