2015-12-10 21 views
2

未遂1 - 在「borg.h」我有這個功能調用函數形式應用

BORG_Problem BORG_Problem_create(
    int numberOfVariables, 
    int numberOfObjectives, 
    int numberOfConstraints, 
    void (*function)(double*, double*, double*)) { 
BORG_Validate_positive(numberOfVariables); 
BORG_Validate_positive(numberOfObjectives); 
BORG_Validate_positive(numberOfConstraints); 
BORG_Validate_pointer((void*)function); 

BORG_Problem problem = (BORG_Problem)malloc(sizeof(struct BORG_Problem_t)); 

BORG_Validate_malloc(problem);`enter code here` 

我在Form1.h

構建Windows窗體應用程序

Form1。 H包括:

#include "borg.h" 
... 


    namespace MO_TLN_NETWORK_GUI 
{ 

     using namespace System; 
     using namespace System::ComponentModel; 
     using namespace System::Collections; 
     using namespace System::Windows::Forms; 
     using namespace System::Data; 
     using namespace System::Drawing; 
     using namespace System::IO; 
     using namespace System::Runtime::InteropServices; 
     public ref class Form1 : public System::Windows::Forms::Form 
    { 


//Click button in Form1 
private: System::Void btnRun_Click(System::Object^ sender,  System::EventArgs^ e) 
      { 
      int invars = 8; //Number of variables 
      int inobjs = 2; //Number of variables 
      int inconst = 1; //Number of constraints 

      BORG_Problem problem = BORG_Problem_create(invars, inobjs, inconst, test_problem); 
      } 

//test_problem function in Form1 
private: void test_problem (double *xreal, double *obj, double *constr) 
      { 
      ... 
      } 
    }; 
} 

這在編譯檢索1個錯誤:

1>c:\c\borg\mo_tln_network_gui\Form1.h(497) : error C3867:'MO_TLN_NETWORK_GUI::Form1::test_problem': function call missing argument list; use '&MO_TLN_NETWORK_GUI::Form1::test_problem' to create a pointer to member

ATTEMPT 2 - 然後我代替來電BORG_Problem_create:

BORG_Problem problem = BORG_Problem_create(invars, inobjs, inconst, &MO_TLN_NETWORK_GUI::Form1::test_problem); 

但這產生另一個錯誤:

..error C3374: can't take address of 'MO_TLN_NETWORK_GUI::Form1::test_problem' unless creating delegate instance

未遂3 - 一些尋求一些帖子後,我試圖

//Before namespace MO_TLN_NETWORK_GUI, I inserted: 
public delegate void MyDel(double *xreal, double *obj, double *constr); 

,並呼籲BORG_Problem_create

  Form1 ^a = gcnew Form1; //OK 
     MyDel^DelInst = gcnew MyDel(a, &MO_TLN_NETWORK_GUI::Form1::test_problem); //OK 
     BORG_Problem problem = BORG_Problem_create(invars, inobjs, inconst, DelInst); //ERROR!!!!!!!!!!! 

..error C2664: 'BORG_Problem_create' : cannot convert parameter 4 from 'MyDel ^' to 'void (__cdecl *)(double *,double *,double *)' No user-defined-conversion operator available, or There is no context in which this conversion is possible

+0

這是C++/CLI嗎?如果是這樣,請標記爲這樣。 – CoryKramer

+0

對不起,但我不知道回答你的問題。我已在Visual Studio 2008由於 –

+0

例如編程'Form1中^ A'不是純C++,該字符''^是[指示C++/CLI的](https://stackoverflow.com/questions/500580/in- c-cli-what-do-hat-c​​haracter-do)代碼,它是C++的擴展。 – CoryKramer

回答

-1

你介意也改變函數頭的委託:

BORG_Problem BORG_Problem_create(
int numberOfVariables, 
int numberOfObjectives, 
int numberOfConstraints, 
MyDel ^function){} 

,你也可以在你的代碼,用於創建對象的管理指令gcnew所以垃圾收集器管理您所有的對象。

例如:

BORG_Problem^ problem = gcnew BORG_Problem(); 

但因此structclass還具有進行管理。

+0

佛羅多,你能更具體地說我該怎麼做?謝謝 –

+0

對於你的「錯誤C2664」,我認爲你已經忘記了更改你的'borg.h'文件中的代碼。所以要確保函數'BORG_Problem_create()'的頭部也有委託作爲參數,不再是'void(* function)(double *,double *,double *)'。此外,BORG_Validate_pointer((void *)函數)必須在'BORG_Validate_pointer(MyDel ^)'中更改。 – Frodo

+0

我在這裏解決我的問題:https://social.msdn.microsoft.com/Forums/pt-BR/d6c72787-0bf5-4c25-9dcf-a2951fcbb397/call-function-defined-as-class-member-inside-other - 函數式窗口形式的應用程序?論壇= vcgeneral –