函数从窗体单位和非窗体单位返回值 [英] Function return value from form unit and non-form unit
问题描述
以前,我能够得到结果,但进一步扩大了我的理解其工作。
Unit1.h
#ifndef Unit1H
#define Unit1H
// ------------------- -------------------------------------------------- ------
#include< System.Classes.hpp>
#include< Vcl.Controls.hpp>
#include< Vcl.StdCtrls.hpp>
#include< Vcl.Forms.hpp>
// -------------------------------------------- -------------------------------
class TForm1:public TForm
{
__published :// IDE管理的组件
TButton * Button1;
TButton * Button2;
TLabel * Label1;
TLabel * Label2;
void __fastcall Button1Click(TObject * Sender);
void __fastcall Button2Click(TObject * Sender);
private://用户声明
public://用户声明
__fastcall TForm1(TComponent * Owner);
};
// -------------------------------------------- -------------------------------
extern PACKAGE TForm1 * Form1;
// -------------------------------------------- -------------------------------
#endif
Unit1.cpp
-------------------------------------------------- --------------------
#include< vcl.h>
#pragma hdrstop
#includeUnit1.h
#includeunit2.h
#includeUnit3.h
// ---------------------------------------------- -----------------------------
#pragma package(smart_init)
#pragma resource* .dfm
TForm1 * Form1;
// -------------------------------------------- -------------------------------
int lengthOfYard;
int widthOfYard;
int regionOfYard;
__fastcall TForm1 :: TForm1(TComponent * Owner)
:TForm(Owner)
{
}
// --- -------------------------------------------------- ----------------------
void __fastcall TForm1 :: Button1Click(TObject * Sender)
{
Form2 = new TForm2 (这个);
widthOfYard = getWidth();
lengthOfYard = getLength();
// widthOfYard = 15;
// lengthOfYard = 17;
Form2-> ShowModal();
}
// ----------------------------------- ----------------------------------------
void __fastcall TForm1 :: Button2Click (TObject * Sender)
{
areaOfYard = FindArea(lengthOfYard,widthOfYard);
Label2-> Caption =\\\
Your yard is+ String(areaOfYard)+square feet\\\
\\\
;
}
// ----------------------------------- ----------------------------------------
unit2.h
-------------------------------------------------- -----------------------
#ifndef Unit2H
#define Unit2H
// --- -------------------------------------------------- ----------------------
#include< System.Classes.hpp>
#include< Vcl.Controls.hpp>
#include< Vcl.StdCtrls.hpp>
#include< Vcl.Forms.hpp>
// -------------------------------------------- -------------------------------
class TForm2:public TForm
{
__published :// IDE-managed Components
private://用户声明
public://用户声明
__fastcall TForm2(TComponent * Owner);
int getLength();
int getWidth();
};
// -------------------------------------------- -------------------------------
extern PACKAGE TForm2 * Form2;
// -------------------------------------------- -------------------------------
#endif
unit2.cpp
-------------------------------------------------- --------------------
#include< vcl.h>
#pragma hdrstop
#includeUnit2.h
// ---------------------- -------------------------------------------------- ---
#pragma package(smart_init)
#pragma resource* .dfm
TForm2 * Form2;
// ---------------------------------------- -----------------------------------
__fastcall TForm2 :: TForm2(TComponent * Owner)
:TForm(Owner)
{
}
// -------------------------- -------------------------------------------------
int getLength()
{
return 15;
}
// ----------------------------------- ----------------------------------------
int getWidth ()
{
return 17;
}
// ----------------------------------- ----------------------------------------
Unit3.h
-------------------------------------------------- -----------------------
#ifndef Unit3H
#define Unit3H
// ------- -------------------------------------------------- ------------------
//在头文件中声明这里的函数。
int FindArea(int length,int width); // function prototype
#endif // ---------------------------------- ---------------------------------- -------
unit3.cpp
-------------------------------------------------- ------------------------
#pragma hdrstop
#includeUnit3.h
//------------------------------------------- -------------------------------
#pragma package(smart_init)
int FindArea(int l,int w)
{
return l * w;
}
我得到的错误是
[bcc32错误] Unit1.cpp(31):E2268调用未定义函数'getLength'
和
[bcc32错误] Unit1.cpp(32):E2268调用未定义的函数'getWidth'
我不能理解为我将这些新函数的声明和定义与 c> getLength()
$
getWidth()
在翻译单元中,这两个函数不可见(他们是in Unit2.cpp
。查看什么是外部链接和内部链接
-
add
extern int getLength();
extern int getWidth(); Unit1.cpp
文件开头的
。 extern
关键字表示外部链接(即,函数的定义可能预期来自某个其他C ++文件)。编译器将相信你已经在某处实现了函数。如果链接器找不到它,你将得到一个链接器错误。
-
BUT
您应该注意到您的 getLength()
函数不是 TForm2 :: getLength()
成员函数。
要定义 TForm2 :: getLength
您必须写:
int TForm2 :: getLength()
{
return 15;
}
(您还应该包括 Unit2.h Unit1.cpp
)
调用 TForm2的语法: :getLength()
是:
Form2-> getLength()
通常,不访问私有/受保护的数据成员的成员函数没有什么意义(你应该更喜欢免费功能)。
Previously I am able to get the result, but took it further to broaden my understanding its workings.
Unit1.h
#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <System.Classes.hpp>
#include <Vcl.Controls.hpp>
#include <Vcl.StdCtrls.hpp>
#include <Vcl.Forms.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TButton *Button1;
TButton *Button2;
TLabel *Label1;
TLabel *Label2;
void __fastcall Button1Click(TObject *Sender);
void __fastcall Button2Click(TObject *Sender);
private: // User declarations
public: // User declarations
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
Unit1.cpp
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
#include "unit2.h"
#include "Unit3.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
int lengthOfYard;
int widthOfYard;
int areaOfYard;
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
Form2 = new TForm2(this);
widthOfYard = getWidth();
lengthOfYard = getLength();
//widthOfYard = 15;
//lengthOfYard = 17;
Form2->ShowModal();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
areaOfYard= FindArea(lengthOfYard,widthOfYard);
Label2->Caption = "\nYour yard is "+ String(areaOfYard) +" square feet\n\n";
}
//---------------------------------------------------------------------------
unit2.h
//---------------------------------------------------------------------------
#ifndef Unit2H
#define Unit2H
//---------------------------------------------------------------------------
#include <System.Classes.hpp>
#include <Vcl.Controls.hpp>
#include <Vcl.StdCtrls.hpp>
#include <Vcl.Forms.hpp>
//---------------------------------------------------------------------------
class TForm2 : public TForm
{
__published: // IDE-managed Components
private: // User declarations
public: // User declarations
__fastcall TForm2(TComponent* Owner);
int getLength();
int getWidth();
};
//---------------------------------------------------------------------------
extern PACKAGE TForm2 *Form2;
//---------------------------------------------------------------------------
#endif
unit2.cpp
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit2.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm2 *Form2;
//---------------------------------------------------------------------------
__fastcall TForm2::TForm2(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
int getLength()
{
return 15;
}
//---------------------------------------------------------------------------
int getWidth()
{
return 17;
}
//---------------------------------------------------------------------------
Unit3.h
//---------------------------------------------------------------------------
#ifndef Unit3H
#define Unit3H
//---------------------------------------------------------------------------
// declare the function here in the header file.
int FindArea(int length, int width); //function prototype
#endif//-------------------------------------------------------------------- -------
unit3.cpp
//---------------------------------------------------------------------------
#pragma hdrstop
#include "Unit3.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
int FindArea(int l, int w)
{
return l * w;
}
I got the error like
[bcc32 Error] Unit1.cpp(31): E2268 Call to undefined function 'getLength'
and
[bcc32 Error] Unit1.cpp(32): E2268 Call to undefined function 'getWidth'
I cannot figure it as I compared these new functions' declarations and definitions with "FindArea" function.
解决方案 You're calling getLength()
and getWidth()
from within Unit1.cpp
.
In that translation unit, these two functions aren't visible ("they're in" Unit2.cpp
. Take a look at What is external linkage and internal linkage in C++).
You have two options:
add
extern int getLength();
extern int getWidth();
at the beginning of Unit1.cpp
file. The extern
keyword indicates external linkage (i.e. the definition of the function may be expected to come from some other C++ file). The compiler will trust that you have implemented the function somewhere. If the linker cannot find it, you will get a linker error.
declare the two functions in Unit2.h
and include this header file in Unit1.cpp
(for further details: C++ extern keyword on functions. Why no just include the header file?).
BUT
you should note that your getLength()
function isn't the implementation of the TForm2::getLength()
member function.
To define TForm2::getLength()
you have to write:
int TForm2::getLength()
{
return 15;
}
(you should also include Unit2.h
inside Unit1.cpp
)
The syntax to call TForm2::getLength()
is:
Form2->getLength()
Usually a member function that doesn't access private/protected data members doesn't make much sense (you should prefer a free function).
这篇关于函数从窗体单位和非窗体单位返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!