实例化时如避免重复定义
choge 2010-12-21 06:56:48 新建一个工程,放一个按钮Button1。
新建一个Unit2,定义一个类ALM,初始化ALM时动态创建一个窗体。
在Unit2.h实例化一个类test
要求点Button1时,显示动态创建的窗体。
build时出现报警
[Linker Warning] Public symbol '_test' defined in both module C:\新建文件夹\UNIT1.OBJ and C:\新建文件夹\UNIT2.OBJ
好像是说重复定义,如何解决,请高手帮忙分析
Unit1.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)
{
test.Show();
}
//---------------------------------------------------------------------------
Unit1.h
//---------------------------------------------------------------------------
#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ExtCtrls.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TButton *Button1;
void __fastcall Button1Click(TObject *Sender);
private: // User declarations
public: // User declarations
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
Unit2.cpp
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit2.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
ALM::ALM()
{
Form1 = new TForm((void*)(0));
}
void ALM::Show()
{
Form1->Visible=true;
}
void ALM::Close()
{
Form1->Visible=false;
}
Unit2.h
//---------------------------------------------------------------------------
#ifndef Unit2H
#define Unit2H
//---------------------------------------------------------------------------
class ALM
{
public:
ALM();
void Show();
void Close();
private:
TForm *Form1;
};
ALM test;
#endif