2015-06-25 50 views
2

嗨我不知道如何调用函数在无形或非形式单位返回值。从无形或非形式单位的函数返回值

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; 
} 

我无法找到此问题的解决方案或教程。 你有更好的吗? Clement

+0

目前的文件组织看起来很好。你有任何警告/错误信息?你关心什么? – manlio

+0

在Unit1.cpp中,错误是“Unit1.cpp(29):parsing:void _fastcall TForm1 :: Button2Click(TObject *)”突出显示错误“Label2-> Caption =”\ n您的代码是“+ areaOfYard +”square脚\ n \ n “;”。编译时发生错误。 – user1739825

回答

0

当前文件组织很好。

你应该改变行:

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更多详情请参阅Concatenate two string literals)。

如果使用String,则operator+将被重载,并且将指向char的指针添加到String已被很好地定义。

EDIT

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

#include <windows.h> 
Unit3.h

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

+0

嗨,谢谢。在我修正之后,新的错误出现了。我似乎只做了一个函数的多个声明。我不确定我犯了什么错误。错误是“[bcc32错误] Unit3.h(9):E2141声明语法错误 完整解析器上下文 Unit3.cpp(5):#include Unit3.h [bcc32 Error] Unit3.cpp(9):E2238 Multiple declaration 'USHORT' [bcc32错误] Unit3.h(9):E2344早先声明'USHORT'“。 – user1739825

+0

'USHORT'在'windows.h'中定义,可能在'Unit3.cpp'里面是未知的(答案中有更多细节)......或者你已经在某处定义了USHORT类型(如此多重声明)。 – manlio

+0

谢谢。我将Ushort更改为int。 – user1739825