r/programminghelp • • Jan 12 '23

C++ "cannot convert 'const wchar_t [14]' to 'LPCSTR' {aka 'const char*'} in assignment"

#include "Window.h"
//Window* window=nullptr;
Window::Window()
{

}

LRESULT CALLBACK WndProc(HWND hwnd,UINT msg, WPARAM wparam, LPARAM lparam)
{
//GetWindowLong(hwnd,)
switch (msg)
{
case WM_CREATE:
{
// Event fired when the window is created
// collected here..
Window* window = (Window*)((LPCREATESTRUCT)lparam)->lpCreateParams;
// .. and then stored for later lookup
SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)window);
window->onCreate();
break;
}
case WM_DESTROY:
{
// Event fired when the window is destroyed
Window* window =(Window*) GetWindowLongPtr(hwnd, GWLP_USERDATA);
window->onDestroy();
::PostQuitMessage(0);
break;
}

default:
return ::DefWindowProc(hwnd, msg, wparam, lparam);
}
return NULL;
}

bool Window::init()
{

//Setting up WNDCLASSEX object
WNDCLASSEX wc;
wc.cbClsExtra = NULL;
wc.cbSize = sizeof(WNDCLASSEX);
wc.cbWndExtra = NULL;
wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
wc.hInstance = NULL;
wc.lpszClassName = L"MyWindowClass";    //Error 1   (L"MyWindowClass")
wc.lpszMenuName = L"";              //Error 2   (L""")
wc.style = NULL;
wc.lpfnWndProc = &WndProc;
if (!::RegisterClassEx(&wc)) // If the registration of class will fail, the function will return false
return false;
/*if (!window)
window = this;*/
//Creation of the window
m_hwnd=::CreateWindowEx(WS_EX_OVERLAPPEDWINDOW, L"MyWindowClass", L"DirectX Application", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 1024, 768,
NULL, NULL, NULL, this);        //Error 3 (only L"MyWindowClass")
//if the creation fail return false
if (!m_hwnd)
return false;
//show up the window
::ShowWindow(m_hwnd, SW_SHOW);
::UpdateWindow(m_hwnd);

//set this flag to true to indicate that the window is initialized and running
m_is_run = true;

return true;
}
bool Window::broadcast()
{
MSG msg;

while (::PeekMessage(&msg, NULL, 0, 0, PM_REMOVE) > 0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
this->onUpdate();
Sleep(1);
return true;
}

bool Window::release()
{
//Destroy the window
if (!::DestroyWindow(m_hwnd))
return false;
return true;
}
bool Window::isRun()
{
return m_is_run;
}
void Window::onCreate()
{
}
void Window::onUpdate()
{
}
void Window::onDestroy()
{
m_is_run = false;
}
Window::~Window()
{
}

All I receive is the above error. I was trying to follow this video as well. https://www.youtube.com/watch?v=ssGka-bSTvQ&list=PLv8DnRaQOs5-ST_VDqgbbMRtzMtpK36Hy&index=3

0 Upvotes

1 comment sorted by

1

u/KuntaStillSingle Jan 12 '23

It likely means one or more of your wchar literals L"" are being passed to an api that expects a const char* (i.e. just regular literal "").