简体   繁体   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;
  ...

The above is in the .h file以上是在.h文件中

Now I am in the .cpp and I want a main() , where I already prepared a struct ReportGroupRequest and the logins array, to call ReportRequest .现在我在.cpp ,我想要一个main() ,我已经在那里准备了一个struct ReportGroupRequest和 logins 数组,来调用ReportRequest

How to call that function?如何调用该函数?

Documentation says:文档说:

" * " *

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 
   )

Parameters req参数要求

[in] The ReportGroupRequest structure that describes the request parameters. [in] 描述请求参数的 ReportGroupRequest 结构。 logins登录

[in] The list of logins for requesting information. [in] 用于请求信息的登录列表。 total全部的

[out] The number of records returned by the function. [out] 函数返回的记录数。

Return Value返回值

If successful, the method returns a pointer to an array of TradeRecord structures describing trade records.如果成功,该方法将返回一个指向描述交易记录的 TradeRecord 结构数组的指针。 The number of records is added to 'total'.记录数被添加到“总数”中。 In case of failure, it returns NULL.* "如果失败,则返回 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
//+------------------------------------------------------------------+

Thanks a lot, Davide非常感谢,大卫

As I said in the comments the main issue is how to get a CManagerInterface object.正如我在评论中所说,主要问题是如何获取CManagerInterface对象。 Now I can see the whole header I see that there's a CManagerFactory class.现在我可以看到整个标题,我看到有一个CManagerFactory类。 The purpose of that class is to create CManagerInterface objects该类的目的是创建CManagerInterface对象

So you need something like this所以你需要这样的东西

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

You have already created the reports request parameters ( ... ) so you can fill that part in yourself.您已经创建了报告请求参数 ( ... ),因此您可以自己填写该部分。

Now there's a few things here I don't understand.现在有几件事我不明白。 I don't know what version should be, I guess you could just try 0 and see what happens.我不知道应该是什么version ,我想你可以试试0看看会发生什么。

The CManagerFactory has an optional parameter to some library path, ie CManagerFactory对某个库路径有一个可选参数,即

CManagerFactory factory(path);

but again I have no idea what that should be or if it's needed at all.但同样我不知道那应该是什么,或者是否需要它。

Hopefully that will get you somewhere.希望这会带你到某个地方。

EDIT编辑

Maybe something like this will be a bit better, I've added some error handling, and my best guess at how you get the version.也许这样的事情会好一点,我添加了一些错误处理,以及我对如何获得版本的最佳猜测。

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