未遂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
這是C++/CLI嗎?如果是這樣,請標記爲這樣。 – CoryKramer
對不起,但我不知道回答你的問題。我已在Visual Studio 2008由於 –
例如編程'Form1中^ A'不是純C++,該字符''^是[指示C++/CLI的](https://stackoverflow.com/questions/500580/in- c-cli-what-do-hat-character-do)代碼,它是C++的擴展。 – CoryKramer