由于我来自Python,有些事情对我来说还是新鲜事。 得到这个奇怪的问题,我开始感到愚蠢,因为我无法解决它,即使成功地声明我的变量" hwnd"它仍然会引发错误,非常奇怪,如果您需要更多详细信息,请随时提出。
#include <iostream>
#include <TlHelp32.h>
#include <windows.h>
class GetHandleAndBase
{
private:
HWND hwnd;
DWORD procID;
HANDLE handle;
public:
GetHandleAndBase();
~GetHandleAndBase();
// Setting the "hwnd" to a open window
hwnd = FindWindow(NULL, L"Task Manager"); // <=====Error is under hwnd
(此声明没有存储类或类型说明符)
我可能还应该提一下功能
GetWindowThreadProcessId(hwnd, &procID);
稍后在我的课堂上弄乱了错误:&#39; GetWindowThreadProcessId&#39;的功能定义找不到
答案 0 :(得分:2)
分配和初始化是C ++中的不同概念。因此,每个可能出现的各种范围也不总是相同的。最终,hwnd = FindWindow(NULL, L"Task Manager");
是一个声明,可能不会出现在类范围内。
但默认成员初始值设定项可能出现在类范围内,因此:
class GetHandleAndBase
{
private:
HWND hwnd = FindWindow(NULL, L"Task Manager");
//...
完全没问题。但是,如果你需要在初始化GetHandleAndBase
(C ++类的奇数名称,btw)中运行几个语句,你应该在构造函数体内编写它。