1

Visual Studio C++2010を使用しています。

メインスレッド以外から、メインフォームクラスの外部で宣言された関数から、GUIでスレッドセーフな変更を行いたい。これが私が持っているいくつかのコードです:

メインクラスの外:

public delegate void DEBUGDelegate(String^ text);

(...)

int lua_debug(lua_State *L){
    // boolean debug(message)
    Globals^ Global = gcnew Globals;
    String^ debugMsg = gcnew String(lua_tostring(L, 1));

    DEBUGDelegate^ myDelegate = gcnew DEBUGDelegate(Global->FORM, &Form1::DEBUGDelegateMethod);
    Global->FORM->Invoke(myDelegate, gcnew array<Object^> { "HEYO! \r\n" });

    lua_pushboolean(L, true);
    return 1;
}

メインクラスの内部:

public ref class Form1 : public System::Windows::Forms::Form
{

(...)

    public: void DEBUGDelegateMethod(String^ text)
    {
            this->DEBUGBOX->Text += text;
    }

(...)

    private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e)
    {
        Globals^ Global = gcnew Globals;
        Global->FORM = this;
    }

    private: System::Void button1_Click_1(System::Object^  sender, System::EventArgs^  e)
    {
        Globals^ Global = gcnew Globals;
        DEBUGDelegate^ myDelegate = gcnew DEBUGDelegate(this, &Form1::DEBUGDelegateMethod);
        this->Invoke(myDelegate, gcnew array<Object^> { "HEYO! \r\n" });
    }
}

したがって、問題は、関数 "lua_debug"にコメントを付けても、残りの部分が出力されないままの場合、正常に機能し、button1をクリックするとデバッグテキストボックスにテキストが表示されることです。lua_debugでパーツのコメントを解除すると、エラーが発生します。

d:\prog\c++\x\x\Form1.h(146): error C2653: 'Form1' : is not a class or namespace name
1>d:\prog\c++\x\x\Form1.h(146): error C2065: 'DEBUGDelegateMethod' : undeclared identifier
1>d:\prog\c++\x\x\Form1.h(146): error C3350: 'X::DEBUGDelegate' : a delegate constructor expects 2 argument(s)

146行は次のとおりです。

DEBUGDelegate^ myDelegate = gcnew DEBUGDelegate(Global->FORM, &Form1::DEBUGDelegateMethod);

================================================== = @EDIT

Form1宣言の後にlua_debugを移動した後、次のエラーが発生します。

d:\prog\c++\x\x\Form1.h(1829): error C2440: 'initializing' : cannot convert from    'System::Windows::Forms::Form ^' to 'X::Form1 ^'
1>          No user-defined-conversion operator available, or
1>          Cast from base to derived requires safe_cast or static_cast
1>d:\prog\c++\x\x\Form1.h(1829): error C3754: delegate constructor: member function    'X::Form1::DEBUGDelegateMethod' cannot be called on an instance of type 'System::Windows::Forms::Form ^'

列をなして:

DEBUGDelegate^ myDelegate = gcnew DEBUGDelegate(Global->FORM, &Form1::DEBUGDelegateMethod);

Global->FORMは次のように宣言されます。

static Form^ FORM;

グローバルクラスで。

4

1 に答える 1

0

lua_debug'の定義を'の下に移動するForm1か、のように前方宣言Form1してみてくださいDEBUGDelegateMethod。両方とも同じ名前空間(lua_debugおよびForm1)内にあると想定しています。そうでない場合は、146行目で名前空間をプリプリメントする必要があります&YourNamespace::Form1::DEBUGDelegateMethod

編集:(OPの編集による)

ただダウンキャスト:

DEBUGDelegate^ myDelegate = gcnew DEBUGDelegate( dynamic_cast<X::Form1^>(Global->FORM ), &Form1::DEBUGDelegateMethod);
于 2012-08-07T19:36:03.830 に答える