Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++;在回调中使用函数时未知重写说明符 我正在使用C++和Windows API,只需要一小会儿时间。自从昨天以来,Windows API就要求我安装一个低级别的鼠标事件钩子,所以我从没有使用过C++中的回调。为了让我的代码更简洁,我想把一些代码外包给一个函数,然后我遇到了一个我不理解的问题。我认为这与我没有完全理解回调范围或类似的东西有关。我的代码如下所示,我使用C++11和VS2017:_C++_Windows - Fatal编程技术网

C++;在回调中使用函数时未知重写说明符 我正在使用C++和Windows API,只需要一小会儿时间。自从昨天以来,Windows API就要求我安装一个低级别的鼠标事件钩子,所以我从没有使用过C++中的回调。为了让我的代码更简洁,我想把一些代码外包给一个函数,然后我遇到了一个我不理解的问题。我认为这与我没有完全理解回调范围或类似的东西有关。我的代码如下所示,我使用C++11和VS2017:

C++;在回调中使用函数时未知重写说明符 我正在使用C++和Windows API,只需要一小会儿时间。自从昨天以来,Windows API就要求我安装一个低级别的鼠标事件钩子,所以我从没有使用过C++中的回调。为了让我的代码更简洁,我想把一些代码外包给一个函数,然后我遇到了一个我不理解的问题。我认为这与我没有完全理解回调范围或类似的东西有关。我的代码如下所示,我使用C++11和VS2017:,c++,windows,C++,Windows,鼠手 \ifndef\u包括鼠标手柄__ #包括定义鼠标手柄__ #包括 类鼠标手柄 { 私人: 公众: 静态坐标字符串(点); 鼠标手柄(); ~MouseHandler(); }; #endif/__包括鼠标手柄__ MouseHandler.cpp #包括“stdafx.h” #包括“MouseHandler.h” #包括 #包括 使用名称空间std; HHOOK mouseHook=NULL; _bstr_t MouseHandler::坐标字符串(点) { _bstr_t坐标=“(”

鼠手

\ifndef\u包括鼠标手柄__
#包括定义鼠标手柄__
#包括
类鼠标手柄
{
私人:
公众:
静态坐标字符串(点);
鼠标手柄();
~MouseHandler();
};
#endif/__包括鼠标手柄__
MouseHandler.cpp

#包括“stdafx.h”
#包括“MouseHandler.h”
#包括
#包括
使用名称空间std;
HHOOK mouseHook=NULL;
_bstr_t MouseHandler::坐标字符串(点)
{
_bstr_t坐标=“(”;
_bstr_t xcoordinate=to_字符串(point.x).c_str();
_bstr_t ycoordinate=to_string(point.y).c_str();
坐标+=xcoordinate+”,“+ycoordinate+”;
返回坐标;
}
LRESULT回调MouseHookCallback(int-nCode、WPARAM-WPARAM、LPARAM-LPARAM)
{
MSLLHOOKSTRUCT event=*((MSLLHOOKSTRUCT*)lParam);
如果(nCode==HC\U动作)
{
_bstr_t message=“鼠标动作发生在位置:”;
_bstr_t mouseCoordinates=MouseHandler::coordinates字符串(event.pt);
信息+=鼠标坐标;
}
返回CallNextHookEx(鼠标指针、nCode、wParam、lParam);
}
MouseHandler::MouseHandler()
{
mouseHook=SetWindowsHookEx(WH_MOUSE_LL,MouseHookCallback,NULL,0);
while(GetMessage(NULL,NULL,0,0)!=0);
}
鼠手::~MouseHandler()
{
}
当我尝试编译时,我得到了错误
C3646'coordinarestostring':未知重写说明符
。我上下搜索并询问了我的同事,但我在这方面找不到帮助,所以我希望这里的人能帮助我。提前谢谢

  • 确保指定了正确的数据类型。
    to_string((长双精度)point.x).c_str()

  • 加载库。
    #pragma注释(lib,“comsuppw.lib”)


    #pragma注释(lib,“comsuppwd.lib”)

  • comutil.h
    包含到标题
    CMouseHandler.h

  • <>我在VS2010 C++上在Wi10 1803 x64上测试了你的代码。p>
    CMouseHandler.h

    #pragma once
    #include <Windows.h>
    #include <comutil.h>
    #include <string>
    
    using namespace std;
    
    #pragma comment (lib, "comsuppw.lib")
    
    class CMouseHandler
    {
    public:
        CMouseHandler(void);
        ~CMouseHandler(void);
        static _bstr_t coordinatesToString(POINT point);
    };
    
    #include "CMouseHandler.h"
    
    HHOOK mouseHook = NULL;
    
    _bstr_t CMouseHandler::coordinatesToString(POINT point)
    {
        _bstr_t coordinates = "(";
        _bstr_t xcoordinate = to_string((long double)point.x).c_str();
        _bstr_t ycoordinate = to_string((long double)point.y).c_str();
        coordinates += xcoordinate + "," + ycoordinate + ")";
    
        return coordinates;
    }
    
    LRESULT CALLBACK MouseHookCallback(int nCode, WPARAM wParam, LPARAM lParam)
    {
        MSLLHOOKSTRUCT event = *((MSLLHOOKSTRUCT *)lParam);
    
        if (nCode == HC_ACTION)
        {
            _bstr_t message = "Mouse action happened at position: ";
            _bstr_t mouseCoordinates = CMouseHandler::coordinatesToString(event.pt);
            message += mouseCoordinates;
        }
        return CallNextHookEx(mouseHook, nCode, wParam, lParam);
    }
    
    CMouseHandler::CMouseHandler()
    {
        mouseHook = SetWindowsHookEx(WH_MOUSE_LL, MouseHookCallback, NULL, 0);
        while (GetMessage(NULL, NULL, 0, 0) != 0);
    }
    
    
    CMouseHandler::~CMouseHandler(void)
    {
    }
    

    \u bstr\u t
    未定义,因为visual studio无法识别它假定为重写说明符的类型。您需要按照.

    中的指定包含
    comutil.h
    ,猜测“\u bstr\u t”未定义。完整的错误消息是什么?它是在哪里产生的(在输出窗口中查看,而不是错误列表,因为通常有更多的细节)?我认为可能发生的情况是,
    \include
    应该在.h文件中,这样
    \bstr\t
    对于函数声明是可见的。@AlanBirtles这是我的错误。非常感谢。我试着尽可能有条理地保存这些内容,但有一个内容漏掉了,让我头疼不已。你们中有谁能写下来作为回答,然后我就可以接受了;)@ACRAI5075请阅读上文:)唯一重要的是将`#include移到标题中,其余的不需要(在我更大的项目中)。库已经自动包含了,为什么我必须强制转换这种类型的数据?但无论如何谢谢你的回答:)@LSchad是的,对。我将
    comutil.h
    添加到标题中。我将把它添加到我的帖子中。
    #pragma once
    #include <Windows.h>
    #include <comutil.h>
    #include <string>
    
    using namespace std;
    
    #pragma comment (lib, "comsuppw.lib")
    
    class CMouseHandler
    {
    public:
        CMouseHandler(void);
        ~CMouseHandler(void);
        static _bstr_t coordinatesToString(POINT point);
    };
    
    #include "CMouseHandler.h"
    
    HHOOK mouseHook = NULL;
    
    _bstr_t CMouseHandler::coordinatesToString(POINT point)
    {
        _bstr_t coordinates = "(";
        _bstr_t xcoordinate = to_string((long double)point.x).c_str();
        _bstr_t ycoordinate = to_string((long double)point.y).c_str();
        coordinates += xcoordinate + "," + ycoordinate + ")";
    
        return coordinates;
    }
    
    LRESULT CALLBACK MouseHookCallback(int nCode, WPARAM wParam, LPARAM lParam)
    {
        MSLLHOOKSTRUCT event = *((MSLLHOOKSTRUCT *)lParam);
    
        if (nCode == HC_ACTION)
        {
            _bstr_t message = "Mouse action happened at position: ";
            _bstr_t mouseCoordinates = CMouseHandler::coordinatesToString(event.pt);
            message += mouseCoordinates;
        }
        return CallNextHookEx(mouseHook, nCode, wParam, lParam);
    }
    
    CMouseHandler::CMouseHandler()
    {
        mouseHook = SetWindowsHookEx(WH_MOUSE_LL, MouseHookCallback, NULL, 0);
        while (GetMessage(NULL, NULL, 0, 0) != 0);
    }
    
    
    CMouseHandler::~CMouseHandler(void)
    {
    }