-1

As I'm coming from Python some things are still new to me. Getting this strange problem and I'm starting to feel stupid because I can't solve it, even when successfully declaring my variable "hwnd" it still throws an error, quite strange, if you need more details feel free to ask.

#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

(this declaration has no storage class or type specifier)

I should probably also mention that the function

GetWindowThreadProcessId(hwnd, &procID); 

slightly later on in my class is messing up with the error: Function Definition for 'GetWindowThreadProcessId' not found

1 Answers1

2

Assignment and initialization are different concepts in C++. As such, the various scopes in which each may appear are also not always the same. Ultimately, hwnd = FindWindow(NULL, L"Task Manager"); is a statement, which may not appear in class scope.

But default member initializers may appear in class scope, so this:

class GetHandleAndBase
{
private:
    HWND hwnd = FindWindow(NULL, L"Task Manager");
//...

Would be perfectly fine. If however you need to run several statements as part of initializing GetHandleAndBase (odd name for a C++ class, btw), you should write that inside a constructors body.

StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458
  • 1
    Or an [IIFE](http://www.bfilipek.com/2016/11/iife-for-complex-initialization.html), that can work too. – Quentin Feb 13 '18 at 13:00