外部函数问题请教

hificat2 2006-09-27 11:16:20
在unit1.cpp中有一个自定义函数,我想在另一个文件中用到的话,大家说这样对吗,我试了有问题。
1.在unit1.cpp中自定好函数a extern void a()
2.在unit1.h中的public:// User declarations
__fastcall TForm1(TComponent* Owner);
void a(void);
3.在unit2.cpp中加入#include unit1.h

但是程序在执行过程中,在unit2.cpp中调用a函数的语句时,仍出现找不到函数定义的出错信息。
...全文
91 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
hificat2 2006-09-27
  • 打赏
  • 举报
回复
嗯,可以了,定义在类外面就可以了,还有,我开发一个全局变量,也定义在unit1.h中:char Buffer_A[4096];
但在form2中buttion事件中修改了它的值,只能在button范围内有效,当再点击button时又为原来外部的值了,是不是再次按button后,取Buffer_A的值只能是原来老的?
BlueDeepOcean 2006-09-27
  • 打赏
  • 举报
回复
哎,看这个,Form1和Form2,Form2定义一个MyMessage函数,在Form1的Button1的单击事件中调用,过程如哈:

Form1.cpp

//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
#include "Unit2.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
Form2->MyMessage();
}
//---------------------------------------------------------------------------


Form2.h

//---------------------------------------------------------------------------

#ifndef Unit2H
#define Unit2H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
//---------------------------------------------------------------------------
class TForm2 : public TForm
{
__published: // IDE-managed Components
private: // User declarations
public: // User declarations
__fastcall TForm2(TComponent* Owner);
void __fastcall TForm2::MyMessage(void)
{
ShowMessage("Hello World!");
}
};
//---------------------------------------------------------------------------
extern PACKAGE TForm2 *Form2;
//---------------------------------------------------------------------------
#endif
hificat2 2006-09-27
  • 打赏
  • 举报
回复
但编译后出现一个错误:
Unresolved external 'TForm1::a()' referenced from xxx.obj
lurel 2006-09-27
  • 打赏
  • 举报
回复
你一定写错地方了,你应该写到类的外面才可以的。
microboy 2006-09-27
  • 打赏
  • 举报
回复
unit1.h:
//....
public:
__fastcall TForm1(TComponent* Owner);
void a(void);
//....

unit2.cpp中#include "unit1.h"
访问a函数的方法为

Form1->a();

13,825

社区成员

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

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