Vector 和 HWND 零内存错误 c++
Vector and HWND Zero memory error c++
我想创建多个 windows ..所以与其一遍又一遍地写它们,我决定使用 vector 来保存所有 HWND 的列表并更新它们作为程序 proceeds.But 它似乎没有按预期工作。它给我一个访问冲突错误。
我尝试初始化 vector.But 似乎没有任何效果
WindoManager.h
#include<windowsx.h>
#include<Windows.h>
#include<vector>
#include<list>
#include<iostream>
using namespace std;
class WindowManager{
// Constructor
WindowManager();
// Destructor
~WindowManager();
private:
vector<HWND> wnds;
HWND hwnd;
public:
void CreateNewWindow(HINSTANCE hinst,WNDCLASSEX * ex,HWND * Parent,HWND * ReturnWind,int Width,int Height,int X,int Y);
void ShowWindows(int);
int HandleMseeages();
};
WindowManager.cpp
#include"WindowManager.h"
WindowManager::WindowManager()
{
}
void WindowManager::CreateNewWindow(HINSTANCE hinst,WNDCLASSEX * ex,HWND * Parent,HWND * ReturnWind,int Width,int Height,int X,int Y)
{
if(Parent == NULL)
{
hwnd = CreateWindowEx(NULL,ex->lpszClassName,ex->lpszMenuName,WS_OVERLAPPEDWINDOW,X,Y,Width,Height,NULL,NULL,hinst,NULL);
wnds.push_back(hwnd);
*ReturnWind = hwnd;
}
}
void WindowManager::ShowWindows(int show)
{
for(vector<HWND>::iterator it = wnds.begin();it != wnds.end() ; ++it)
{
ShowWindow(*it,show);
}
}
int WindowManager::HandleMseeages()
{
MSG msg;
for(int i=0;i< wnds.size();++i)
{
if(PeekMessage(&msg,wnds[i],0,0,PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
if(msg.message == WM_QUIT)
return 0;
}
}
return 1;
}
并且函数从 source.cpp
调用
....//Removed unwanted code
WindowManager * WndManager = {0};
WndManager->CreateNewWindow(hinst,&classex,NULL,&hwnd,500,500,0,0);
WndManager->ShowWindows(cmdshow);
我不知道怎么了..请帮帮我
谢谢
试试这个:
WindowManager WndManager{};
WndManager.CreateNewWindow(hinst,&classex,NULL,&hwnd,500,500,0,0);
WndManager.ShowWindows(cmdshow);
问题似乎出在这一行:
WindowManager * WndManager = {0};
您声明了一个指针,但没有分配对象并调用它的构造函数。
分配对象为:
WindowManager * WndManager = new WindowManager;
或者只是简单地在堆栈中定义它并使用正确的调用(如 Richard Hodges 所建议):
WindowManager WndManager;
WndManager.CreateNewWindow(hinst,&classex,NULL,&hwnd,500,500,0,0);
WndManager.ShowWindows(cmdshow);
我想创建多个 windows ..所以与其一遍又一遍地写它们,我决定使用 vector 来保存所有 HWND 的列表并更新它们作为程序 proceeds.But 它似乎没有按预期工作。它给我一个访问冲突错误。
我尝试初始化 vector.But 似乎没有任何效果
WindoManager.h
#include<windowsx.h>
#include<Windows.h>
#include<vector>
#include<list>
#include<iostream>
using namespace std;
class WindowManager{
// Constructor
WindowManager();
// Destructor
~WindowManager();
private:
vector<HWND> wnds;
HWND hwnd;
public:
void CreateNewWindow(HINSTANCE hinst,WNDCLASSEX * ex,HWND * Parent,HWND * ReturnWind,int Width,int Height,int X,int Y);
void ShowWindows(int);
int HandleMseeages();
};
WindowManager.cpp
#include"WindowManager.h"
WindowManager::WindowManager()
{
}
void WindowManager::CreateNewWindow(HINSTANCE hinst,WNDCLASSEX * ex,HWND * Parent,HWND * ReturnWind,int Width,int Height,int X,int Y)
{
if(Parent == NULL)
{
hwnd = CreateWindowEx(NULL,ex->lpszClassName,ex->lpszMenuName,WS_OVERLAPPEDWINDOW,X,Y,Width,Height,NULL,NULL,hinst,NULL);
wnds.push_back(hwnd);
*ReturnWind = hwnd;
}
}
void WindowManager::ShowWindows(int show)
{
for(vector<HWND>::iterator it = wnds.begin();it != wnds.end() ; ++it)
{
ShowWindow(*it,show);
}
}
int WindowManager::HandleMseeages()
{
MSG msg;
for(int i=0;i< wnds.size();++i)
{
if(PeekMessage(&msg,wnds[i],0,0,PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
if(msg.message == WM_QUIT)
return 0;
}
}
return 1;
}
并且函数从 source.cpp
调用....//Removed unwanted code
WindowManager * WndManager = {0};
WndManager->CreateNewWindow(hinst,&classex,NULL,&hwnd,500,500,0,0);
WndManager->ShowWindows(cmdshow);
我不知道怎么了..请帮帮我
谢谢
试试这个:
WindowManager WndManager{};
WndManager.CreateNewWindow(hinst,&classex,NULL,&hwnd,500,500,0,0);
WndManager.ShowWindows(cmdshow);
问题似乎出在这一行:
WindowManager * WndManager = {0};
您声明了一个指针,但没有分配对象并调用它的构造函数。
分配对象为:
WindowManager * WndManager = new WindowManager;
或者只是简单地在堆栈中定义它并使用正确的调用(如 Richard Hodges 所建议):
WindowManager WndManager;
WndManager.CreateNewWindow(hinst,&classex,NULL,&hwnd,500,500,0,0);
WndManager.ShowWindows(cmdshow);