2012-08-07 61 views
1

之外我正在usinng的Visual Studio C++ 2010調用從類.NET

我想在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; 
在全局類

+0

貌似lua_debug功能無法看到Form1的類型(第一個錯誤表明這一點,其他兩個是第一個的結果)。我認爲有些信息缺失。 Form1是否在命名空間中定義? lua_debug和Form1在同一個文件中?哪一個先走? #包括?編譯指示? – 2012-08-07 19:13:18

+0

Form1在名稱空間內部定義,lua_debug和Form1都在Form1.h文件中,lua_debug首先聲明,然後是Form1 - 在由Form1上的buttonclick創建的新線程中調用lua_debug。 – user1558211 2012-08-07 19:22:45

回答

0

嘗試移動lua_debug的定義低於Form1's或正向聲明Form1正如您對DEBUGDelegateMethod所做的那樣。我假設它們都在同一個命名空間(lua_debugForm1)之內。如果不是,則在第146行中,您必須預先命名空間,即:&YourNamespace::Form1::DEBUGDelegateMethod

編輯:(按OP的編輯)

就垂頭喪氣:

DEBUGDelegate^ myDelegate = gcnew DEBUGDelegate(dynamic_cast<X::Form1^>(Global->FORM), &Form1::DEBUGDelegateMethod); 
+0

有新的錯誤:錯誤C2440:'初始化':無法從'System :: Windows :: Forms :: Form ^'轉換爲'X :: Form1 ^' – user1558211 2012-08-07 20:09:47

+0

查看我的更新回答 – 2012-08-07 20:52:57