函数返回值来自无形或非形式单元

时间:2015-06-25 06:57:59

标签: 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 *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;
}

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

1 个答案:

答案 0 :(得分:0)

当前的文件组织没问题。

您应该更改以下行:

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

成:

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

第一个表单给出了错误:

  

E2885:指针添加无效

由于"\nYour yard is "的类型为char *而您正在向指针添加数字(请查看Borland C++ Builder 6 and string concatenationConcatenate two string literals以获取更多详细信息)。

String operator+被重载,并且String添加了指向char的指针。

修改

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

#include <windows.h>
Unit3.h中的

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