Visual c++ 需要知道如何在另一个cpp文件中正确创建新对象

Visual c++ 需要知道如何在另一个cpp文件中正确创建新对象,visual-c++,Visual C++,我有课。现在的问题是,经过几次尝试后,我仍然犯下了巨大的错误。我的问题是我不知道如何在另一个cpp文件中为这个类正确地声明一个新对象。我想从另一个cpp文件中调用/触发这个RebarHandler类中的函数。我不断遇到诸如“未经初始化就使用”、“调试断言失败”等问题 在另一个cpp文件中,我包含了RebarHandler.h,并这样做: CRebarHandler *test=NULL; test->setButtonMenu2(); 编译时,我不会给出任何错误。但是,当运行时,它会导致

我有课。现在的问题是,经过几次尝试后,我仍然犯下了巨大的错误。我的问题是我不知道如何在另一个cpp文件中为这个类正确地声明一个新对象。我想从另一个cpp文件中调用/触发这个RebarHandler类中的函数。我不断遇到诸如“未经初始化就使用”、“调试断言失败”等问题

在另一个cpp文件中,我包含了RebarHandler.h,并这样做:

CRebarHandler *test=NULL;
test->setButtonMenu2();
编译时,我不会给出任何错误。但是,当运行时,它会导致错误和IE崩溃。我需要帮助

下面是我的意思:

    #pragma once

    class CIEWindow;

    class CRebarHandler  : public CWindowImpl<CRebarHandler>{
    public:
CRebarHandler(HWND hWndToolbar, CIEWindow *ieWindow);
CRebarHandler(){};

~CRebarHandler();

BEGIN_MSG_MAP(CRebarHandler)
    NOTIFY_CODE_HANDLER(TBN_DROPDOWN, onNotifyDropDown)
    NOTIFY_CODE_HANDLER(TBN_TOOLBARCHANGE, onNotifyToolbarChange)
    NOTIFY_CODE_HANDLER(NM_CUSTOMDRAW, onNotifyCustomDraw)
    NOTIFY_CODE_HANDLER(TBN_ENDADJUST, onNotifyEndAdjust)
    MESSAGE_HANDLER(WM_SETREDRAW, onSetRedraw)
END_MSG_MAP()

// message handlers
LRESULT onNotifyDropDown(WPARAM wParam, LPNMHDR pNMHDR, BOOL& bHandled); 
LRESULT onNotifyToolbarChange(WPARAM wParam, LPNMHDR pNMHDR, BOOL& bHandled);
LRESULT onNotifyCustomDraw(WPARAM wParam, LPNMHDR pNMHDR, BOOL& bHandled);
LRESULT onNotifyEndAdjust(WPARAM wParam, LPNMHDR pNMHDR, BOOL& bHandled);
LRESULT onSetRedraw(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);

// manage the subclassing of the IE rebar
void subclass();
void unsubclass();
void handleSettings();
    void setButtonMenu2();
bool findButton(HWND hWndToolbar);

    private:
// handles to the various things
HWND m_hWnd;
HWND m_hWndToolbar, m_hWndRebar, m_hWndTooltip;
HMENU m_hMenu;

int m_buttonID;
int m_ieVer;

CIEWindow *m_ieWindow;

// toolbar finding functions
void scanForToolbarSlow();
void getRebarHWND();
void setButtonMenu();
#pragma一次
类CIEWindow;
类CRebarHandler:public CWindowImpl{
公众:
CRebarHandler(HWND HWND工具栏,CIEWindow*ieWindow);
CRebarHandler(){};
~CRebarHandler();
开始消息映射(CRebarHandler)
NOTIFY_CODE_处理程序(TBN_下拉列表、OnNotify下拉列表)
NOTIFY_CODE_处理程序(TBN_TOOLBARCHANGE,onNotifyToolbarChange)
NOTIFY_CODE_处理程序(NM_CUSTOMDRAW,onNotifyCustomDraw)
NOTIFY_CODE_处理程序(TBN_ENDADJUST,onNotifyEndAdjust)
消息处理程序(WM_SETREDRAW、onSetRedraw)
END_MSG_MAP()
//消息处理程序
LRESULT OnNotify下拉列表(WPARAM WPARAM、LPNMHDR pNMHDR、BOOL和bHandled);
LRESULT OND变更(WPARAM WPARAM、LPNMHDR pNMHDR、BOOL和bHandled);
LRESULT onNotifyCustomDraw(WPARAM WPARAM、LPNMHDR pNMHDR、BOOL和bHandled);
LRESULT onNotifyEndAdjust(WPARAM WPARAM、LPNMHDR pNMHDR、BOOL和bHandled);
LRESULT onSetRedraw(UINT uMsg、WPARAM WPARAM、LPARAM LPARAM、BOOL和bHandled);
//管理IE钢筋的子类化
void子类();
void unsubclass();
无效句柄设置();
void setButtonMenu2();
布尔查找按钮(HWND HWND工具栏);
私人:
//处理各种事情
HWND m_HWND;
HWND m_HWND工具栏、m_hWndRebar、m_HWND工具提示;
胡敏胡敏;
int m_buttonID;
国际货币基金组织;
CIEWindow*m_视窗;
//工具栏查找功能
void scanForToolbarSlow();
void getRebarHWND();
void setButtonMenu();

})

不管CRebarHandler做什么,这些行都不好:

CRebarHandler *test=NULL; 
test->setButtonMenu2(); 
你有这个指针,你先说“它不指向任何东西”,然后说“继续,用指针指向的东西,设置按钮菜单。”这是不可能的

尝试:

取决于测试所需的生存期。您可能希望使用接受参数的构造函数,而不是默认构造函数。我的观点是,您必须有一个
CRebarHandler
,才能对其调用方法

CRebarHandler test; 
test.setButtonMenu2(); 
CRebarHandler test= new CRebarHandler(); 
test->setButtonMenu2();