函数 return 从无形单位到有形单位的值

Function return value from formless unit thru form unit

嗨,我在这里学到了很多关于使用函数的知识。 我学到的很多东西是:- - 函数 return 来自另一种形式的值 - 函数 return 来自另一个无形单位的值

这一次,我希望函数从无形式单元通过另一种形式获取报价。

使用上面的这些方法,我尝试从无形式单元 THROUGH 中获取函数 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 *Button3;
TLabel *Label3;
void __fastcall Button3Click(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 "Unit2.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;

extern String getQuote();

String quote1;


//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}

//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
quote1 = Form2->getQuote();
Label3->Caption = quote1;
}
//---------------------------------------------------------------------------

Unit2.h

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

#ifndef Unit2H
#define Unit2H
//---------------------------------------------------------------------------
#include <System.Classes.hpp>
#include <Vcl.Controls.hpp>
#include <Vcl.StdCtrls.hpp>
#include <Vcl.Forms.hpp>

extern String FindQuote();

//---------------------------------------------------------------------------
class TForm2 : public TForm
{
__published:    // IDE-managed Components
private:    // User declarations
public:     // User declarations
__fastcall TForm2(TComponent* Owner);

String getQuote();
};
//---------------------------------------------------------------------------
extern PACKAGE TForm2 *Form2;
//---------------------------------------------------------------------------
#endif

Unit2.cpp

//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include "Unit2.h"
#include "Unit3.h"

extern String FindQuote();

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm2 *Form2;

//---------------------------------------------------------------------------
__fastcall TForm2::TForm2(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------

String TForm2::getQuote()
{
    return FindQuote();
}

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

Unit3.h

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

#ifndef Unit3H
#define Unit3H

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

// declare the function here in the header file.

String FindQuote();

#endif

Unit3.cpp

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

#pragma hdrstop

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

String FindQuote()
{
    return "He with whom neither slander that gradually soaks into the mind, nor statements that startle like a wound in the flesh, are successful may be called intelligent indeed.";
}

我收到错误“

[bcc32 Error] Unit3.h(11): E2141 Declaration syntax error
  Full parser context
Unit3.cpp(5): #include Unit3.h
[bcc32 Error] Unit3.cpp(14): E2238 Multiple declaration for 'String'
[bcc32 Error] Unit3.h(11): E2344 Earlier declaration of 'String'
[bcc32 Error] Unit3.cpp(14): E2141 Declaration syntax error

"。如何解决?

Unit1.cpp 不应该有 extern String getQuote(); 。反正没有那个名字的自由函数,你永远不会调用它。

Unit2.hUnit2.cpp 都不应该有 extern String FindQuote(); 。相反,Unit2.cpp 应该 #include "Unit3.h"(它确实如此)以便它看到 String FindQuote();.

如果您不知道,extern 是多余的,extern X f();X f(); 相同。


您的错误是 String 尚未声明。在任何使用 String 的代码之前,您必须让 #include <vcl.h> 可见。您可以将它放在 Unit3.h 的顶部,或者将它放在 Unit3.cpp#pragma hdrstop 之前。

您应该在刚刚显示的消息之前收到其他 error/warning 消息,提醒您 String 未声明。在继续担心以后的错误之前,请始终解决您遇到的第一个 error/warning,因为它们可能是从第一个错误中级联而来的虚假消息。