如何调用另一个cpp中变量的值(有代码)

laohei0304 2013-03-18 11:48:06
我想调用TFormMaster.cpp中的pQuery的值,在Query.cpp中使用。
我已经互加了头文件,可以调用变量,但是我想要的是变量的值,求大师相助。

TFormMaster.cpp
void __fastcall TFormMaster::Open1Click(TObject *Sender)
{

//----------------------------------------------------------------------------------// - 打开文件复制内容至 recvbuf[1024]
// 指向文件内容指针 pfile
//----------------------------------------------------------------------------------
AnsiString S; char recvbuf[128*1024];
TStringList *Str=new TStringList();
if (OpenDialog1->Execute())
{
Str->LoadFromFile(OpenDialog1->FileName);
S=AnsiString(Str->Text);
}
memset(recvbuf,0x00,sizeof(recvbuf));
strcpy(recvbuf,S.c_str());
pfile = recvbuf;
pQuery = recvbuf;
//----------------------------------------------------------------------------------
}


Query.cpp
void __fastcall TQuiry::Button2Click(TObject *Sender)
{
if(ComboBox1->Text =="变比")
{
memcpy(buffer32, pQuery+=40, 4);
}
}
...全文
1135 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
ccrun.com 2013-03-18
  • 打赏
  • 举报
回复
将 pQuery 在.h文件中声明: extern 类型 pQuery; 然后在.cpp文件中定义: 类型 pQuery; 然后在需要使用pQuery的单元中包含上面说的.h文件。
laohei0304 2013-03-18
  • 打赏
  • 举报
回复
多谢了,您人真好。
ccrun.com 2013-03-18
  • 打赏
  • 举报
回复
原来你把recvbuf定义成类的成员了,这样就用不着extern声明了。直接在Query单元中,以TFormMater对象的成员方式来访问就行了,如: FormMaster->recvbuf 当然,FormMaster得保证已经创建并且只有一个,否则得到的数据将不是你预期的。
laohei0304 2013-03-18
  • 打赏
  • 举报
回复
TFormMaster.h //--------------------------------------------------------------------------- #ifndef TFormMasterH #define TFormMasterH //--------------------------------------------------------------------------- #include <Classes.hpp> #include <Controls.hpp> #include <StdCtrls.hpp> #include <Forms.hpp> #include <Grids.hpp> #include <Dialogs.hpp> #include <Menus.hpp> #include <DB.hpp> #include <DBTables.hpp> #include <DBCtrls.hpp> #include <ExtCtrls.hpp> //--------------------------------------------------------------------------- class TFormMaster : public TForm { __published: // IDE-managed Components TOpenDialog *OpenDialog1; TSaveDialog *SaveDialog1; TMainMenu *MainMenu1; void __fastcall Button3Click(TObject *Sender); void __fastcall Button2Click(TObject *Sender); void __fastcall Exit1Click(TObject *Sender); void __fastcall Open1Click(TObject *Sender); void __fastcall Save1Click(TObject *Sender); void __fastcall BUFFERClick(TObject *Sender); void __fastcall DISPLAYClick(TObject *Sender); void __fastcall Button1Click(TObject *Sender); private: // User declarations public: // User declarations __fastcall TFormMaster(TComponent* Owner); char* pfile; char* pQuery; char recvbuf[1024]; }; //--------------------------------------------------------------------------- extern PACKAGE TFormMaster *FormMaster; //--------------------------------------------------------------------------- #endif TFormMaster.cpp #include <vcl.h> #pragma hdrstop #include <stdio.h> #include <stdlib.h> #include "BAPProtocol.h" #include "BAPCommon.h" #include "TFormMaster.h" #include "Query.h" //---------------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TFormMaster *FormMaster; static TBAPRECORD Record; static PBAPRECORD pRecord; static char str[128]; static char Buff8[128]; //存放8位的数据 static char Buff16[128]; //存放16位的数据 static char Buff32[128]; //存放32位的数据 TStringList *Str=new TStringList(); //---------------------------------------------------------------------------------- __fastcall TFormMaster::TFormMaster(TComponent* Owner) : TForm(Owner) { } //---------------------------------------------------------------------------------- void __fastcall TFormMaster::Open1Click(TObject *Sender) { //---------------------------------------------------------------------------------// - 打开文件复制内容至 recvbuf[1024] // 指向文件内容指针 pfile //---------------------------------------------------------------------------------- AnsiString S; char recvbuf[128*1024]; if (OpenDialog1->Execute()) { Str->LoadFromFile(OpenDialog1->FileName); S=AnsiString(Str->Text); } memset(recvbuf,0x00,sizeof(recvbuf)); strcpy(recvbuf,S.c_str()); pfile = recvbuf; pQuery = recvbuf; //--------------------------------------------------------------------------- } //--------------------------------------------------------------------------- 下面是Query.cpp Query.h //--------------------------------------------------------------------------- #ifndef QueryH #define QueryH //--------------------------------------------------------------------------- #include <Classes.hpp> #include <Controls.hpp> #include <StdCtrls.hpp> #include <Forms.hpp> //--------------------------------------------------------------------------- class TQuiry : public TForm { __published: // IDE-managed Components TComboBox *ComboBox1; TButton *Button1; TComboBox *ComboBox2; TComboBox *ComboBox3; TComboBox *ComboBox4; TButton *Button2; void __fastcall ComboBox1Change(TObject *Sender); void __fastcall Button1Click(TObject *Sender); void __fastcall Button2Click(TObject *Sender); private: // User declarations public: // User declarations __fastcall TQuiry(TComponent* Owner); }; //--------------------------------------------------------------------------- extern PACKAGE TQuiry *Quiry; //--------------------------------------------------------------------------- #endif Query.cpp #include <vcl.h> #pragma hdrstop #include "TFormMaster.h" #include "BAPProtocol.h" #include "BAPCommon.h" #include "Query.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TQuiry *Quiry; static TBAPRECORD Record; int MaxSize = sizeof(FormMaster->recvbuf); //--------------------------------------------------------------------------- __fastcall TQuiry::TQuiry(TComponent* Owner) : TForm(Owner) { ComboBox1->Items->Add("变比"); ComboBox1->Items->Add("极性"); ComboBox1->Items->Add("直阻"); ComboBox1->Items->Add("一次参考"); ComboBox1->Items->Add("二次参考"); } //--------------------------------------------------------------------------- void __fastcall TQuiry::Button2Click(TObject *Sender) { if(ComboBox1->Text =="变比") { memcpy(Buff32, pQuery+=40, 4); Record.gRatioInfo.RatioMeasure_Ratio = atof(Buff32); } } 这就是其中的代码,我就是想在Query中使用TFormMater中的recvbuf。
ccrun.com 2013-03-18
  • 打赏
  • 举报
回复
请贴完整代码
laohei0304 2013-03-18
  • 打赏
  • 举报
回复
引用 1 楼 ccrun 的回复:
将 pQuery 在.h文件中声明: extern 类型 pQuery; 然后在.cpp文件中定义: 类型 pQuery; 然后在需要使用pQuery的单元中包含上面说的.h文件。
使用extern不能通过编译,extern not allowed here

13,825

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧