繁体   English   中英

main.cpp 从 .h 文件调用公共虚拟方法

[英]main.cpp calling public virtual method from .h file

  class CManagerInterface
  {
      public:
      //--- reports
      virtual TradeRecord* __stdcall ReportsRequest(const ReportGroupRequest 
      *req,const int *logins,int *total)    =0;
  ...

以上是在.h文件中

现在我在.cpp ,我想要一个main() ,我已经在那里准备了一个struct ReportGroupRequest和 logins 数组,来调用ReportRequest

如何调用该函数?

文档说:

" *

CManagerInterface::ReportsRequest
//Gets information about closed positions of clients in order to generate a custom reports.
TradeRecord*  CManagerInterface::ReportsRequest( 
   const ReportGroupRequest*  req,     // Request 
   const int*                 logins,  // List of logins 
   int*                       total    // Number of received records 
   )

参数要求

[in] 描述请求参数的 ReportGroupRequest 结构。 登录

[in] 用于请求信息的登录列表。 全部的

[out] 函数返回的记录数。

返回值

如果成功,该方法将返回一个指向描述交易记录的 TradeRecord 结构数组的指针。 记录数被添加到“总数”中。 如果失败,则返回 NULL.*"

//+------------------------------------------------------------------+
//| Functions                                                        |
//+------------------------------------------------------------------+
typedef int (*MtManVersion_t)(void);
typedef int (*MtManCreate_t)(int version,CManagerInterface **man);
//+------------------------------------------------------------------+
//| Factory                                                          |
//+------------------------------------------------------------------+
#ifndef _MT4MANDLL
class CManagerFactory
  {
private:
   HMODULE           m_lib;
   MtManVersion_t    m_pfnManVersion;
   MtManCreate_t     m_pfnManCreate;
public:
   //--- constructor
   CManagerFactory(LPCSTR lib_path=NULL):m_lib(NULL)
     {
      Init(lib_path);
     }
   //--- destructor
  ~CManagerFactory()
     {
      if(m_lib)
        {
         m_pfnManVersion=NULL;
         m_pfnManCreate =NULL;
         ::FreeLibrary(m_lib);
         m_lib=NULL;
        }
     }
   //--- initialization
   inline void Init(LPCSTR lib_path=NULL)
     {
      char path[256]="";
      //---
      if(lib_path!=NULL)
        {
         strcpy(path,lib_path);
         path[sizeof(path)-1]=0;
        }
      else
        {
         #ifndef _WIN64
         strcpy_s(path,"mtmanapi.dll");
         path[sizeof(path)-1]=0;
         #else
         strcpy(path,"mtmanapi64.dll");
         path[sizeof(path)-1]=0;
         #endif
        }
      //---
      if(m_lib)
         ::FreeLibrary(m_lib);
      if((m_lib=::LoadLibraryA(path))!=NULL)
        {
         m_pfnManVersion=reinterpret_cast<MtManVersion_t>(::GetProcAddress(m_lib,"MtManVersion"));
         m_pfnManCreate =reinterpret_cast<MtManCreate_t>(::GetProcAddress(m_lib,"MtManCreate"));
        }
      else
        {
         m_pfnManVersion=NULL;
         m_pfnManCreate =NULL;
        }
      //---
     }
   //--- winsock startup/cleanup
   inline int WinsockStartup() const
     {
      WSADATA wsa;
      return(WSAStartup(0x0202,&wsa)!=0 ? RET_ERROR:RET_OK);
     }
   inline void WinsockCleanup() const
     {
      WSACleanup();
     }
   //---
   inline int IsValid() const
     {
      return(m_lib!=NULL && m_pfnManVersion!=NULL && m_pfnManCreate!=NULL) ? TRUE:FALSE;
     }
   inline int Version() const
     {
      return(m_pfnManVersion?(*m_pfnManVersion)():0);
     }
   inline CManagerInterface* Create(const int version) const
     {
      CManagerInterface *man=NULL;
      if(m_pfnManCreate) (*m_pfnManCreate)(version,&man);
      return(man);
     }
  };
#endif
//+------------------------------------------------------------------+

非常感谢,大卫

正如我在评论中所说,主要问题是如何获取CManagerInterface对象。 现在我可以看到整个标题,我看到有一个CManagerFactory类。 该类的目的是创建CManagerInterface对象

所以你需要这样的东西

CManagerFactory factory;
CManagerInterface* intf = factory.Create(version);
TradeRecord* records = intf->ReportsRequest(...);

您已经创建了报告请求参数 ( ... ),因此您可以自己填写该部分。

现在有几件事我不明白。 我不知道应该是什么version ,我想你可以试试0看看会发生什么。

CManagerFactory对某个库路径有一个可选参数,即

CManagerFactory factory(path);

但同样我不知道那应该是什么,或者是否需要它。

希望这会带你到某个地方。

编辑

也许这样的事情会好一点,我添加了一些错误处理,以及我对如何获得版本的最佳猜测。

CManagerFactory factory;
if (!factory.IsValid())
    cerr << "invalid factory\n";
int version = factory.Version();
CManagerInterface* intf = factory.Create(version);
if (!intf)
    cerr << "could not create interface\n";
TradeRecord* records = intf->ReportsRequest(...);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM