函数从表单单元和非表单单元返回值

时间:2015-06-26 07:47:57

标签: c++ c++builder

以前我能够得到结果,但是进一步扩展了我对其工作的理解。

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”函数进行了比较。

1 个答案:

答案 0 :(得分:0)

您从getLength()内拨打getWidth()Unit1.cpp

在那个translation unit中,这两个功能不可见(&#34;他们在&#34; Unit2.cpp。看看What is external linkage and internal linkage?

您有两种选择:

  • 添加

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

    Unit1.cpp文件的开头。 extern关键字表示外部链接(即函数的定义可能来自某些其他C ++文件)。编译器将相信您已在某处实现了该功能。如果链接器找不到它,则会出现链接器错误。

  • Unit2.h中声明这两个函数,并在Unit1.cpp中包含此标题文件(有关详细信息:C++ extern keyword on functions. Why no just include the header file?)。

<强> BUT

您应该注意,getLength()函数不是TForm2::getLength()成员函数的实现。

要定义TForm2::getLength(),你必须写:

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

(您还应该在Unit2.h内包含Unit1.cpp

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

Form2->getLength()

通常,无法访问私人/受保护数据成员的成员函数没有多大意义(您应该更喜欢free function)。