Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/161.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++;在file.h和file.cpp中编写回调函数_C++_Windows_Callback - Fatal编程技术网

C++ c++;在file.h和file.cpp中编写回调函数

C++ c++;在file.h和file.cpp中编写回调函数,c++,windows,callback,C++,Windows,Callback,我需要编写回调函数,因为我将使用以下函数: EnumChildWindows(windowHandle, EnumChildProc, 0); 其中第二个变量“EnumChildProc”需要是回调函数 // .h file class file { private: HWND m_hwParent; static HRESULT myFunction (HWND hw, LPARAM arg1); public: HRESULT On

我需要编写回调函数,因为我将使用以下函数:

EnumChildWindows(windowHandle, EnumChildProc, 0);
其中第二个变量“EnumChildProc”需要是回调函数

  // .h file

  class file 
  { 
   private:

   HWND m_hwParent;

   static HRESULT myFunction (HWND hw, LPARAM arg1); 

   public:

   HRESULT OnAllChildWindows (LPARAM arg1);

   // more stuff
  };

  // .cpp file 

  HRESULT file::myFunction (HWND hw, LPARAM arg1) 
  {
   // whatever this does
  }

  HRESULT file::OnAllChildWindows (LPARAM arg1)
  {
   EnumChildWindows (m_hwParent, myFunction, arg1);
  }
问题是在头文件和源文件中编写函数时遇到困难。通常,当我编写布尔函数或任何其他类型时,我会首先以以下方式在标题中删除它:

bool myFunction(int var1);
如果前一个文件在file.h中,我将在源文件中编写以下代码:

bool file::myFunction (int var1)
{
  ///here would be my code
}
但使用回调函数时,我尝试了以下操作,但没有成功:

在文件.h中:

BOOL CALLBACK EnumChildProc(HWND windowHandle, LPARAM lParam);
在文件.cpp中:

BOOL file::CALLBACK EnumChildProc(HWND windowHandle, LPARAM lParam)
{
   ///code goes here
}
它不工作,因为我得到以下错误:

error: expected unqualified-id before '__attribute__'
错误引用源代码中的以下行:

BOOL programLoop::CALLBACK EnumChildProc(HWND windowHandle, LPARAM lParam)

你知道我做错了什么吗?当我在头文件、源文件或其他地方声明回调函数时,我不知道我是否做错了什么。。。你们怎么看?

回调只是一个宏,它在函数声明中添加了专门的Microsoft属性。仅在声明中使用,而不是在使用函数的地方使用

  // .h file

  class file 
  { 
   private:

   HWND m_hwParent;

   static HRESULT myFunction (HWND hw, LPARAM arg1); 

   public:

   HRESULT OnAllChildWindows (LPARAM arg1);

   // more stuff
  };

  // .cpp file 

  HRESULT file::myFunction (HWND hw, LPARAM arg1) 
  {
   // whatever this does
  }

  HRESULT file::OnAllChildWindows (LPARAM arg1)
  {
   EnumChildWindows (m_hwParent, myFunction, arg1);
  }
EnumChildWindows的第二个参数声明为
WNDENUMPROC
,声明为

    typedef BOOL (CALLBACK* WNDENUMPROC)(HWND, LPARAM);
因此,您可以将回调函数声明为

    BOOL CALLBACK myFunction (HWND w, LPARAM var1);

作用域限定符
始终位于作用域名称(在本例中为
file
)和从属名称(
myFunction
)之间

如果
文件
是一个名称空间,那么您就都设置好了;如果
file
是一个类,请记住还要在类中声明myFunction
static
。在某些情况下,您可能还需要
extern

枚举子窗口时

    HWND hwparent=/* handle of the window you want to enumerate */;
    LPARAM var1=/* the value you want to pass in*/;

    EnumChildWindows (hwParent, myFunction, var1);
Windows为每个窗口调用函数,传入
w
参数,以便知道它是哪个窗口,以及
var1
哪个是您的参数


至于where:不需要在头文件中声明
myFunction
。很可能只有对
EnumChildWindows
的调用使用
myFunction
。它不会从任何其他文件调用。因此,只要
myFunction
在.cpp文件中调用
EnumChildWindows
之前,您就可以了

当然,如果
file
是一个类,那么静态成员函数
myFunction
必须位于类声明中,该声明通常位于头的某个位置

使用该函数时,如果调用它的位置不在
文件
(类或命名空间)范围内,则只需添加
文件::

如果
file
是一个类,则需要在其中定义myFunction的
file::
,但如果在另一个类成员函数中调用
EnumChildWindows
,则不需要
file::

  // .h file

  class file 
  { 
   private:

   HWND m_hwParent;

   static HRESULT myFunction (HWND hw, LPARAM arg1); 

   public:

   HRESULT OnAllChildWindows (LPARAM arg1);

   // more stuff
  };

  // .cpp file 

  HRESULT file::myFunction (HWND hw, LPARAM arg1) 
  {
   // whatever this does
  }

  HRESULT file::OnAllChildWindows (LPARAM arg1)
  {
   EnumChildWindows (m_hwParent, myFunction, arg1);
  }
如果
file
是不需要的命名空间
file::
如果将所有代码包装在命名空间块中:

  namespace file {

      HRESULT myFunction (HWND hw, LPARAM arg1) 
      {
       // whatever this does
      }

      HRESULT OnAllChildWindows (HWND hwParent, LPARAM arg1)
      {
       EnumChildWindows (myFunction, arg1);
      }
   }
但是如果你没有,或者如果它是一个不同的范围:

  HRESULT someting_else::OnAllChildWindows (HWND hwParent, LPARAM arg1)
  {
   EnumChildWindows (hwParent, file::myFunction, arg1);
  }

您不需要在cpp中的函数定义前面加上
文件::
。定义类成员函数时,需要在其前面加上
classname::
;类名可能(并且经常)与filname相同,但不一定非得如此。回调函数是一个自由函数,因此它不需要任何作用域(除非在涉及名称空间的特殊情况下)。另外,如果回调函数没有从其他文件引用,则根本不需要在.h中声明它。好的。。。“类成员函数”和“自由函数”有什么区别。我只是写下了文件::总是,但我甚至不明白它是什么purpose@dlf哦,如果我不在文件中声明,就会有问题。h。。。它似乎没有得到我在file.h头的public部分声明的变量。。。你怎么看?自由函数只是一个不属于任何类的函数。像C++和java这样的语言不允许这样做,但是C++确实如此。任何用于EnumChildWindows之类的回调函数都必须是自由函数或静态成员函数。这意味着如果不做一些额外的工作,您将无法从回调函数中获取任何类成员变量。好吧,太棒了,这就是我在头文件中声明它的方式。您将如何在.cpp文件中写入它。。。同时使用file::myFunction还是其他方式?(如果可以,请显示示例代码)评论太长,所以我将其添加到我的答案中。