英文:
How to access a variable in another window?
问题
我试图通过在Form1
中调用Form2
的Funkcja()
函数来访问Form2
中的变量,但我遇到了“访问冲突”错误。在这种情况下,错误消息如下:
Ustring.cpp:Project Project1.exe引发了异常类0xc0000005,消息为“在地址0x175ee0处遇到异常0xc0000005:访问位置0x000006d8”。
无论String A
在Form2
中是private
还是public
。
UnicodeString& UnicodeString::operator=(const UnicodeString& src)
{
System::Internal::Strhlpr::UnicodeAssign(*this, const_cast<UnicodeString&>(src));
return *this;
}
Unit1.cpp
#include "Unit2.h"
void __fastcall TForm1::Button1Click(TObject *Sender)
{
Form2->Funkcja();
}
Unit2.h
class TForm2 : public TForm
{
__published: // IDE-managed Components
void __fastcall FormClose(TObject *Sender, TCloseAction &Action);
private: // User declarations
public: // User declarations
__fastcall TForm2(TComponent* Owner);
String A;
void __fastcall Funkcja();
};
Unit2.cpp
void __fastcall TForm2::Funkcja()
{
Form2->A = "";
}
英文:
I'm trying to access a variable in Form2
by calling a function Funkcja()
in Form1
, but I'm getting an "access violation" error. In this case it looks like this:
> Ustring.cpp: Project Project1.exe raised exception class 0xc0000005 with message 'Exception 0xc0000005 encountered at address 0x175ee0: Access violation reading location 0x000006d8'.
Whether String A
is in private
or public
in Form2
.
UnicodeString& UnicodeString::operator=(const UnicodeString& src)
{
System::Internal::Strhlpr::UnicodeAssign(*this, const_cast<UnicodeString&>(src));
return *this;
}
Unit1.cpp
#include "Unit2.h"
void __fastcall TForm1::Button1Click(TObject *Sender)
{
Form2->Funkcja();
}
Unit2.h
class TForm2 : public TForm
{
__published: // IDE-managed Components
void __fastcall FormClose(TObject *Sender, TCloseAction &Action);
private: // User declarations
public: // User declarations
__fastcall TForm2(TComponent* Owner);
String A;
void __fastcall Funkcja();
};
Unit2.cpp
void __fastcall TForm2::Funkcja()
{
Form2->A = "";
}
答案1
得分: 1
访问冲突错误意味着您正在通过无效的 TForm2
对象指针访问 A
数据成员。
在 TForm2::Funkcja()
中,您应该使用 this
指针而不是全局的 Form2
指针,例如:
void __fastcall TForm2::Funkcja()
{
this->A = "";
// 或者简单地:
// A = "";
}
如果在进行此更改后代码仍然崩溃,则意味着 TForm1::Button1Click()
使用的 Form2
指针并非指向有效的 TForm2
对象。
例如,如果在调用 Funkcja()
时 Form2
对象尚未创建。或者,如果已经创建,但 Form2
指针没有更新为指向它。提供的上下文不足以诊断错误的根本原因。
英文:
The Access Violation error means you are accessing the A
data member via an invalid TForm2
object pointer.
Inside of TForm2::Funkcja()
, you should be using the this
pointer instead of the global Form2
pointer, eg:
void __fastcall TForm2::Funkcja()
{
this->A = "";
// or simply:
// A = "";
}
If the code still crashes after making that change, then it means the Form2
pointer being used by TForm1::Button1Click()
is not pointing at a valid TForm2
object.
For instance, if the Form2
object hasn't been created yet when Funkcja()
is called on it. Or, if it has been created, but the Form2
pointer was not updated to point at it. There is simply not enough context provided to diagnose the root cause of the error.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论