2015-06-25 78 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