我是按照MVC模式用c ++做的第一个项目。我有一个控制器类Session,它具有管理类的所有功能" ClientTsFrm",一个视图。我想要做的就是将所有发生的事件与视图类进行通信。为此,我使用了观察者模式,实际上是这个实现1
Session.h会话也是一个单身人士。
#pragma once
#include "User.h"
#include "Config.h"
#include "../data/message.h"
#include <list>
#include <cstring>
#include <stdio.h>
#include <cstdlib>
#include "../lib/eventtype.h"
#include "../lib/Subject.h"
#ifndef WX_PRECOMP
#include <wx/wx.h>
#include <wx/frame.h>
#else
#include <wx/wxprec.h>
#endif
#include <wx/richtext/richtextctrl.h>
#include <wx/grid.h>
typedef std::list<UserPTR> UserList;
typedef std::shared_ptr<UserList> UserListPTR;
/*
* Set and get functions
*/
class Session: public Subject<EventTS>{
public:
static Session* Instance();
private:
Session(); // Private so that it can not be called
Session(Session const&){}; // copy constructor is private
Session& operator=(Session const&){}; // assignment operator is private
static Session* m_pInstance;
public:
private:
Session* m_instance;
ConfigPTR m_config;
UserListPTR m_luser;
char* m_translationEngine;
MessageQueuePTR m_pending;
MessageQueuePTR m_queue;
};
Subject.h
//
// Copyright (c) 2013 Juan Palacios juan.palacios.puyana@gmail.com
// Subject to the BSD 2-Clause License
// - see < http://opensource.org/licenses/BSD-2-Clause>
//
#ifndef SUBJECT_H_
#define SUBJECT_H_
#include <functional>
#include <map>
#include <vector>
#include <utility> // for std::forward
template <typename Event>
class Subject
{
public:
Subject()=default;
template <typename Observer>
void registerObserver(const Event& event, Observer&& observer)
{
observers_[event].push_back(std::forward<Observer>(observer));
}
template <typename Observer>
void registerObserver(Event&& event, Observer&& observer)
{
observers_[std::move(event)].push_back(std::forward<Observer>(observer));
}
void notify(const Event& event) const
{
for (const auto& obs : observers_.at(event)) obs();
}
// disallow copying and assigning
Subject(const Subject&)=delete;
Subject& operator=(const Subject&)=delete;
private:
std::map<Event, std::vector<std::function<void()>>> observers_;
};
#endif // SUBJECT_H_
ClientTsFrm.h
#pragma once
#ifdef __BORLANDC__
#pragma hdrstop
#endif
#ifndef WX_PRECOMP
#include <wx/wx.h>
#include <wx/frame.h>
#else
#include <wx/wxprec.h>
#endif
#include "../Data/config.h"
#include "../lib/ClientTS.h"
#include "../data/Session.h"
#include "../data/Message.h"
#include "FrmMailSending.h"
#include "FrmSettingMail.h"
#include "AudioWizard.h"
#include "NationList.h"
#include "LoginWarnings.h"
#include "../ArchiveLog.h"
#include "FrmSaveChat.h"
#include <wx/sizer.h>
#include <wx/wx.h>
#include <wx/timer.h>
#include <wx/stattext.h>
#include <wx/richtext/richtextctrl.h>
#include <wx/textctrl.h>
#include <wx/button.h>
#include <wx/grid.h>
#include "../GlobalVariables.h"
#include "../translateController/translateController.h"
#include "../translateController/translateVariable.h"
#include <list>
//#include "../lib/Observer.h"
#define MENU_ESCI 1800
#define MENU_OPZIONI 1801
#define MENU_SPEECH 1802
class ClientTsFrm : public wxFrame
{
ClientTsFrm(LoginWarnings *warn, wxWindow *parent, wxWindowID id = 1, const wxString &title = wxT("TeamTranslate"),
const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize,
long style = wxCAPTION | wxSYSTEM_MENU | wxCLOSE_BOX | wxRESIZE_BORDER);
virtual ~ClientTsFrm(){};
ClientTsFrm(const ClientTsFrm& tg) {
std::cout << "\tSimpleCat(SimpleCat&) called\n";
}
void updatePanelMsg();
private:
unsigned int curRow; //Initialize Row index
unsigned int curCol; //Initialize Column index
Session* session;
ConfigPTR config;
NationList *nations;
int REFRESHTIMER = 0;
uint64 _sclogID;
wxTimer *WxTimer2;
wxTimer *WxTimer1;
wxButton *btnspeech;
wxRichTextCtrl *txtclient;
wxTextCtrl *txtlingua;
wxStaticText *lbllingua;
wxStaticText *lblnick;
wxTextCtrl *txtnick;
wxRichTextCtrl *txtchat;
wxButton *btnsend;
wxTextCtrl *txtmsg;
wxGrid *gridchat;
wxGrid *gridclient;
wxBoxSizer *sizer;
wxGridSizer *gridsizer;
wxMenuBar *WxMenuBar1;
wxMenu *ID_MNU_FILE_1001_Mnu_Obj;
wxMenu *ID_MNU_OPZIONI_1004_Mnu_Obj;
wxBitmapButton *WxBitmapButton1;
ClientTS clientts;
};
ClientTS.cpp。这是我注册对象的方式
...
session->registerObserver(EventTS::MSG_RCV, std::bind(notifyMSG, *this));
...
void notifyMSG(ClientTsFrm *fn)
{
fn->updatePanelMsg();
}
基本上,我正在注册ClientTSFrm对象。它存储在会话类中,当事件发生时,会话调用&#34; notify&#34;主题类的功能。该函数调用ClientTSFrm(观察者)的函数,该函数也发送对象。最后,此函数调用ClientTSFrm类的函数以更新内容。
如果我编译,则显示此错误:
错误28错误C2664:&#39; void(ClientTsFrm *)&#39; :无法转换参数 1来自&#39; ClientTsFrm&#39;到&#39; ClientTsFrm *&#39; C:\ Program Files (x86)\ Microsoft Visual Studio 12.0 \ VC \ include \ functional 1149 1
https://github.com/juanchopanza/cppblog/blob/master/Patterns/Observer/