函数 return 来自无形式或非形式单位的值

Function return value from formless or non-form unit

您好,我不确定如何将无形式或非形式单元中的函数调用到 return 值。

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 *Button2;
    TLabel *Label2;
    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 "Unit3.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{

}

//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
    USHORT lengthOfYard;
    USHORT widthOfYard;
    USHORT areaOfYard;

    widthOfYard = 15;
    lengthOfYard = 17;
    areaOfYard= FindArea(lengthOfYard,widthOfYard);
    Label2->Caption = "\nYour yard is "+ areaOfYard +" square feet\n\n";

}
//---------------------------------------------------------------------------

Unit3.h

//---------------------------------------------------------------------------
#ifndef Unit3H
#define Unit3H
//---------------------------------------------------------------------------

// declare the function here in the header file.

USHORT FindArea(USHORT length, USHORT width); //function prototype

#endif//---------------------------------------------------------------------------

Unit3.cpp

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

#pragma hdrstop

#include "Unit3.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)

USHORT FindArea(USHORT l, USHORT w)
{
    return l * w;
}

我找不到这个问题的解决方案或教程。 你有更好的吗? 克莱门特

当前的文件组织没有问题。

您应该更改行:

Label2->Caption = "\nYour yard is " + areaOfYard + " square feet\n\n";

进入:

Label2->Caption = "\nYour yard is " + String(areaOfYard) + " square feet\n\n";

第一种形式给出错误:

E2885: Invalid pointer addition

因为 "\nYour yard is " 的类型是 char * 并且您要将数字添加到指针(请查看 Borland C++ Builder 6 and string concatenation and Concatenate two string literals 了解更多详细信息)。

对于 Stringoperator+ 被重载,添加一个指向字符的指针到 String 是明确定义的。

编辑

USHORT 类型在 windows.h 中定义。您应该添加:

#include <windows.h>

in Unit3.h 或者,更好的是,使用标准 C++ 类型 (unsigned FindArea(unsigned l, unsigned w))。