gpt4 book ai didi

c++ - 无形式或非形式单元的函数返回值

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:49:54 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 *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";

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

单元3.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 concatenationConcatenate two string literals 以获取更多详细信息).

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

编辑

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

#include <windows.h>

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

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

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