我正在使用Visual Studio C ++ 2010。
我希望从主线程之外的GUI中进行线程安全更改,从主Form类外部声明的函数。这是我的一些代码:
在主要课程之外:
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-&gt; FORM声明为:
static Form^ FORM;
在Globals课程中。
答案 0 :(得分: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);