gpt4 book ai didi

c++ - 表单单元和非表单单元的函数返回值

转载 作者:太空宇宙 更新时间:2023-11-04 13:31:58 24 4
gpt4 key购买 nike

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

单元1.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";
}

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

单元2.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

单元2.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;
}

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

单元3.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”函数进行比较时,我无法理解。

最佳答案

您正在从 Unit1.cpp 中调用 getLength()getWidth()

在那translation unit ,这两个函数是不可见的(“它们在”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?)。

但是

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

要定义 TForm2::getLength(),您必须这样写:

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

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

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

Form2->getLength()

通常不访问私有(private)/ protected 数据成员的成员函数没有多大意义(您应该更喜欢 free function )。

关于c++ - 表单单元和非表单单元的函数返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31067798/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com