从表格单元和非表格单元函数的返回值(Function return value from form

2019-10-23 12:38发布

以前我能够得到的结果,但把它进一步拓宽了我的理解它的运作。

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;
}

我喜欢的错误

[bcc32 Error] Unit1.cpp(31): E2268 Call to undefined function 'getLength'

[bcc32 Error] Unit1.cpp(32): E2268 Call to undefined function 'getWidth'

因为我比较了这些新功能的声明,并以‘一个findArea’函数定义我想不出它。

Answer 1:

你打电话getLength()getWidth()从内部Unit1.cpp

在该翻译单元 ,这两个功能是不可见的(“他们是” Unit2.cpp 。看看什么是外部链接和内部链接? )。

你有两个选择:

  •  extern int getLength(); extern int getWidth(); 

    在年初Unit1.cpp文件。 所述extern关键字指示外部链接(即,函数的定义可以预期来自其他一些C ++文件)。 编译器会相信你在什么地方实现的功能。 如果连接器不能找到它,你会得到一个连接错误。

  • 在声明的两个函数Unit2.h ,包括在这个头文件Unit1.cpp (对于进一步的细节: ?C ++在功能为什么没有只包含头文件extern关键字 )。

你应该注意到,您getLength()函数不是执行TForm2::getLength()成员函数。

要定义TForm2::getLength()你写:

int TForm2::getLength()
{
  return 15;
}

(你还应该包括Unit2.hUnit1.cpp

调用语法TForm2::getLength()是:

Form2->getLength()

通常不访问私有/受保护的数据成员并没有多大意义的成员函数(你应该更喜欢免费的功能 )。



文章来源: Function return value from form unit and non-form unit