-3

I am trying to make a window in C++ and I included the windows.h but I get an error that 'windows' has not been declared.

I was following the Creating a Window tutorial on the Microsoft Docs, yet I can't seem to get it to work because of this error.

Here is my code so far:

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


using namespace std;

int main()
{
    // Register the window class
    const wchar_t CLASS_NAME[] = L"Sample Window Class";
    windows::WNDCLASS wc = { 0 };
    wc.lpfnWndProc = windows::DefWindowProc;
    wc.hInstance = windows::GetModuleHandle(NULL);
    wc.lpszClassName = CLASS_NAME;
    windows::RegisterClass(&wc);

    const windows::DWORD window_style = windows::WS_OVERLAPPEDWINDOW;
    const windows::DWORD window_ex_style = windows::WS_EX_APPWINDOW;
    const windows::RECT window_rect = { 0, 0, 640, 480 };
    windows::AdjustWindowRectEx(&window_rect, window_style, FALSE, window_ex_style);
    windows::HWND window_handle = windows::CreateWindowEx(
        window_ex_style,
        CLASS_NAME,
        L"Sample Window",
        window_style,
        windows::CW_USEDEFAULT,
        windows::CW_USEDEFAULT,
        window_rect.right - window_rect.left,
        window_rect.bottom - window_rect.top,
        NULL,
        NULL,
        windows::GetModuleHandle(NULL),
        NULL
    );

    windows::ShowWindow(window_handle, windows::SW_SHOWDEFAULT);
    windows::UpdateWindow(window_handle);

    

    return 0;
}

What am I doing wrong?

drescherjm
  • 10,365
  • 5
  • 44
  • 64
  • 5
    Who said `windows` was declared? Why do you think `windows` was declared? The tutorial doesn't say `windows` - it appears that you made it up. – user253751 May 19 '22 at 13:23
  • 3
    I don't think `windows::` is a thing? Just remove it. – HolyBlackCat May 19 '22 at 13:24
  • Where in that tutorial does it say things are in the windows namespace? It's a c api so doesn't use namespaces at all – Alan Birtles May 19 '22 at 13:24
  • 1
    The tutorial you linked to doesn't mention a `windows` class or namespace. – Ted Lyngmo May 19 '22 at 13:24
  • As a side note, I wished that Windows functions could be placed in a separate namespace to avoid pollution. And more importantly, I wish I could import `Windows.h` without those tons of macros like `CreateFile`. – prapin May 19 '22 at 13:28
  • 1
    ***What am I doing wrong?*** Adding `windows::` in front of items from the windows api. There is no such namespace. The windows api that you are using is `c` code which does not have the concept of a namespace – drescherjm May 19 '22 at 13:28
  • You write `using namespace std;` (a [bad practice](https://stackoverflow.com/questions/1452721) BTW), but you didn't try a `using namespace windows;` that would have been useful in that context (if that namespace did exist). – prapin May 19 '22 at 13:31
  • 1
    @YvesDaoust You cannot put `extern "C"` functions in a namespace. And even without that, you would have link errors since namespace are mangled in exported symbols. – prapin May 19 '22 at 13:37
  • @prapin: ooops, so right. I'll delete my comment. –  May 19 '22 at 14:52

1 Answers1

0

I tested the code and I got the error: error C2653: 'windows': is not a class or namespace name

There is no windows namespace in c++. As far as I'm concerned you should remove windows::.

With C++/WinRT, you can call Windows Runtime APIs using Standard C++ data types, including some C++ Standard Library data types. When you want to use c++/cx, you need to access the namespace "windows".

The Windows Runtime type system requires that all public Windows Runtime types, including those in your own code, must be declared in a namespace at namespace scope.

If you want to use C++/CX, you could enable C++/CX by using the /ZW compiler option.

I suggest you could refer to the Doc:Namespaces and Type Visibility (C++/CX )

Jeaninez - MSFT
  • 3,210
  • 1
  • 5
  • 20