2

ATTEMPT 1 - In "borg.h" I have this function

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`

I'm building a Windows Form Application in Form1.h

Form1. h includes:

 #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)
            {
            ...
            }
  };
}

This retrieves a 1 error in compiling:

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 - Then I substitute call to BORG_Problem_create:

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

But this generates another error:

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

ATTEMPT 3 - After some looking to some posts, I tried

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

and to call 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

Deduplicator
  • 44,692
  • 7
  • 66
  • 118

1 Answers1

-1

Did you mind also to change the function header to the delegate:

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

?

And also you could use in your code to create object the managed instruction gcnewso the garbage collector manages all your objects.

For example:

BORG_Problem^ problem = gcnew BORG_Problem();

But therefore the structor classalso has to be managed.

Frodo
  • 749
  • 11
  • 23
  • Frodo, could you be more specific in what should I do? Thanks – Joao Vieira Dec 10 '15 at 16:59
  • For your "error C2664" I thought that you migh have forgotten to change the code in your `borg.h`file. So be sure that your header of the function `BORG_Problem_create()` has also the delegate as parameter and no longer the `void (*function)(double*, double*, double*)`. Also the function `BORG_Validate_pointer((void*)function)` has to be changed in `BORG_Validate_pointer(MyDel^)`. – Frodo Dec 11 '15 at 07:13
  • I solved my problem here: https://social.msdn.microsoft.com/Forums/pt-BR/d6c72787-0bf5-4c25-9dcf-a2951fcbb397/call-function-defined-as-class-member-inside-other-function-in-windows-form-application?forum=vcgeneral – Joao Vieira Dec 16 '15 at 14:46