ATTEMPT 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):错误C3867:'MO_TLN_NETWORK_GUI::Form1::test_problem':缺少函数调用自变量列表;使用'&MO_TLN_NETWORK_GUI::Form1::test_problem'到创建指向成员的指针
尝试2-然后我替换对BORG_Problem_create:的调用
BORG_Problem problem = BORG_Problem_create(invars, inobjs, inconst, &MO_TLN_NETWORK_GUI::Form1::test_problem);
但这会产生另一个错误:
错误C3374:无法获取的地址"MO_TLN_NETWORK_GUI::Form1::test_problem",除非创建委托实例
尝试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!!!!!!!!!!!
错误C2664:"BORG_Problem_create":无法从转换参数4'MyDel^'到'void(__cdecl*)(双*,双*,双重*)'否用户定义的转换运算符可用,或者中没有上下文这种转换是可能的
您是否也介意将函数头更改为委托:
BORG_Problem BORG_Problem_create(
int numberOfVariables,
int numberOfObjectives,
int numberOfConstraints,
MyDel ^function){}
此外,您还可以在代码中使用托管指令gcnew
来创建对象,以便垃圾收集器管理所有对象。
例如:
BORG_Problem^ problem = gcnew BORG_Problem();
但是因此CCD_ 2或CCD_。