使用指针和引用混淆自己,c ++(从WinAPI获取鼠标coords)

时间:2013-03-20 23:33:27

标签: c++ pointers reference

我有一个班级:

class Mouse
{
public:
    int x;
    int y;
    void MouseMove( int x, int y );
    Mouse();
};

我将它作为头文件包含在我的windows.cpp中,其中包含WndProc和WinMain函数。 在其下面包括我声明我的鼠标的静态实例:

#include "Mouse.h"
#include "Game.h"

static Mouse mouse;

我的鼠标方法如下:

#include "Mouse.h"

void Mouse::MouseMove( int x, int y )
{
    this->x = x;
    this->y = y;
}

在我的WndProc上我正在处理WM_MOUSEMOUSE并传递x和y值(其中包含正确的值到我的MouseMove函数:

case WM_MOUSEMOVE:
            {

                int x = ( short )LOWORD( lParam );
                int y = ( short )HIWORD( lParam );


                bool leftButtonDown = wParam & MK_LBUTTON;
                bool rightButtonDown = wParam & MK_RBUTTON;

                mouse.MouseMove( x, y );

            }
            break;

我的MouseMove函数运行并将this-> x设置为x成功,并与y值相同。这就完成了......

现在,在我的Windows循环中,我正在运行我的游戏(theGame.Go):

Game theGame = Game( hWnd );

    MSG msg;

    ZeroMemory( &msg, sizeof(msg) );

    while( msg.message!=WM_QUIT )
    {
       if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
       {
          TranslateMessage( &msg );
          DispatchMessage( &msg );
       }
       else
       {
            theGame.Go();
       }
    }

我的游戏标题如下:

#include "Mouse.h"

    class Game
    {
    public:
        Game( HWND hWnd );
        ~Game();
        void Go();
        void ComposeFrame();

        LPD3DXSPRITE sprite;
        LPDIRECT3DTEXTURE9 gTexture;
        D3DXVECTOR3 pos;
    private:
        D3DGraphics gfx;
    };

我的游戏构造如下:

Game::Game( HWND hWnd )
    :
    gfx( hWnd )
{
    HRESULT result;

    sprite = NULL;
    result = D3DXCreateSprite( gfx.d3dDevice, &sprite );
    assert( !FAILED( result ) );

    gTexture = NULL;
    result = D3DXCreateTextureFromFile( gfx.d3dDevice, "Images/character001.png", &gTexture );
    assert( !FAILED( result ) );

    gfx.d3dDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, TRUE);
};

游戏对象不知道我的windows.cpp中声明的鼠标对象是否存在,无论我在那里全局声明它。所以我想我自己,我需要通过引用将鼠标对象传递给我的Game对象。我首先修改windows循环:

Game theGame = Game( hWnd, &mouse );

    MSG msg;

    ZeroMemory( &msg, sizeof(msg) );

    while( msg.message!=WM_QUIT )
    {
       if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
       {
          TranslateMessage( &msg );
          DispatchMessage( &msg );
       }
       else
       {
            theGame.Go();
       }
    }

现在我在Game.h类中添加了一些参数,以便我有一个对内存的引用,以后可以从中获取* mouse.x:

class Game
{
public:
    Game( HWND hWnd, Mouse &mouse );
...

我回去看看我的窗口循环,在我的电话中有一个波浪形的人:

Game theGame = Game( hWnd, &mouse );

指出:

6   IntelliSense: no instance of constructor "Game::Game" matches the argument list
        argument types are: (HWND, Mouse *) c:\Users\James\Documents\Visual Studio 2012\Projects\Game\Game\Windows.cpp  75  17  Game

我不明白吗?我怎么才改变我的鼠标的一个全局实例并从我该死的游戏对象中调用它:(

2 个答案:

答案 0 :(得分:1)

您已将Game::Game构造函数声明为引用Mouse,但您传递的是指针。

更改为:

Game::Game(HWND hWnd, Mouse* mouse);

或:

Game(hWnd, mouse);

另请注意,当您调用Game构造函数时,实际上是在制作不必要的副本:

Game theGame = Game( hWnd, &mouse );

相反,请将其更改为:

Game theGame( hWnd, &mouse );

答案 1 :(得分:1)

导致错误的问题是,您的Game构造函数将引用带到Mouse

Game( HWND hWnd, Mouse &mouse );

但是,您通过取Mouse的地址将指针传递给mouse

Game theGame = Game( hWnd, &mouse );

您可以更改其中一个:将参数类型更改为指针(Mouse* mouse)或将mouse作为参数传递。

然而,这是拥有全局对象的非典型方法。通过将mouse声明为static,您将为其提供内部链接,并且无法在任何其他翻译单元中访问它。您通常会在单个头文件中包含以下声明,只要您需要访问全局mouse对象,就会包含该文件:

extern Mouse mouse;

然后在单个实现文件中提供定义:

Mouse mouse;