无法将'TCHAR *'转换为'const char *'

时间:2014-01-11 12:48:25

标签: c++

我不知道为什么在运行代码时出现这两个错误。 错误:类型'BOOL(Movement ::)(HWND __ ,LPARAM)'的参数与'BOOL()(HWND __ *,LPARAM)'不匹配 错误:类型'BOOL(Movement ::)(HWND __ ,LPARAM)'的参数与'BOOL()(HWND __ *,LPARAM)'不匹配

cpp文件是这样的:

#include "movement.h"
#include <windows.h>
#include <tchar.h>
#include <iostream>

using namespace std;

#pragma comment(lib, "user32.lib")

Movement::Movement()
{
    xLoc = 10;
    yLoc = 50;
    while(true){
            windowHandle = NULL;
            EnumWindows(MyEnumProc, 0);

            MoveWindow(windowHandle, getXLoc(), getYLoc(), 1220, 930, false);
            Sleep(5);
        }
}
BOOL CALLBACK Movement::MyEnumProc(HWND hWnd, LPARAM lParam)
{
    TCHAR title[500];
    ZeroMemory(title, sizeof(title));

    //string strTitle;

    GetWindowText(hWnd, title, sizeof(title)/sizeof(title[0]));

    //_tprintf(_T("Found window: %s\n"), title);

    //strTitle += title; // Convert to std::string
    if(_tcsstr(title, _T("Firefox")))
    {
        windowHandle = hWnd;
        return FALSE;
    }
    return TRUE;
}
int Movement::getXLoc(){
    if(xLoc<70&&yLoc<=30){
        xLoc += 1;
        Sleep(10);
    }else if(yLoc>30){
        xLoc -= 1;
        Sleep(10);
    }
    return xLoc;
}
int Movement::getYLoc(){
    if(yLoc>10&&xLoc<30){
        yLoc -= 1;
        Sleep(10);
    }else if(xLoc>=30&&yLoc<70){
        yLoc += 1;
        Sleep(10);
    }
    return yLoc;
}

这是头文件:

#ifndef MOVEMENT_H
#define MOVEMENT_H

#include <windows.h>
#include <iostream>

using namespace std;

class Movement
{
public:
    Movement();
protected:
    HWND windowHandle;
    BOOL CALLBACK MyEnumProc(HWND hWnd, LPARAM lParam);
    int xLoc;
    int yLoc;
    int getXLoc();
    int getYLoc();
};

#endif // MOVEMENT_H

1 个答案:

答案 0 :(得分:4)

这里的问题是EnumWindows期望WNDENUMPROC参数是

的typedef
BOOL (*)(HWND, LPARAM);

在您的代码示例中,MyEnumProc的类型为

BOOL (Movement::*)(HWND, LPARAM);

存在差异是因为它是Movement上的实例方法。要解决此问题,您需要将MyEnumProc设为静态方法