访问另一个窗口中的变量?

huangapple go评论64阅读模式
英文:

How to access a variable in another window?

问题

我试图通过在Form1中调用Form2Funkcja()函数来访问Form2中的变量,但我遇到了“访问冲突”错误。在这种情况下,错误消息如下:

Ustring.cpp:Project Project1.exe引发了异常类0xc0000005,消息为“在地址0x175ee0处遇到异常0xc0000005:访问位置0x000006d8”。

无论String AForm2中是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&amp; UnicodeString::operator=(const UnicodeString&amp; src)
{
    System::Internal::Strhlpr::UnicodeAssign(*this, const_cast&lt;UnicodeString&amp;&gt;(src));
    return *this;
}

Unit1.cpp

#include &quot;Unit2.h&quot;

void __fastcall TForm1::Button1Click(TObject *Sender)
{
  Form2-&gt;Funkcja();
}

Unit2.h

class TForm2 : public TForm
{
__published:	// IDE-managed Components
	void __fastcall FormClose(TObject *Sender, TCloseAction &amp;Action);
private:	// User declarations

public:		// User declarations
	__fastcall TForm2(TComponent* Owner);

       String A;
       void __fastcall Funkcja();
};

Unit2.cpp

void __fastcall TForm2::Funkcja()
{
   Form2-&gt;A = &quot;&quot;;
}

答案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-&gt;A = &quot;&quot;;
   // or simply:
   // A = &quot;&quot;;
}

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.

huangapple
  • 本文由 发表于 2023年5月13日 22:12:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76243169.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定