您好我在这里学到了很多关于使用函数的知识。 我学到的很多东西都是: - - 来自另一个表单的函数返回值 - 来自另一个无形单元的函数返回值
这一次,我希望函数能够从无形单元 THROUGH 获得另一种形式的引用。
使用上面的这些方法,我尝试让函数从无形单元 THROUGH 返回另一种形式。似乎我无法让它正常工作。
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
“。如何解决?
答案 0 :(得分:0)
Unit1.cpp不应该有extern String getQuote();
。无论如何,该名称没有自由功能,你永远不会打电话给它。
Unit2.h
和Unit2.cpp
都不应该extern String FindQuote();
。相反,Unit2.cpp
应#include "Unit3.h"
(它确实如此),以便它看到String FindQuote();
。
如果您不知道,extern
是多余的,extern X f();
与X f();
相同。
您的错误是String
尚未声明。在使用#include <vcl.h>
的任何代码之前,您必须看到String
。您可以将其放在Unit3.h
的顶部,也可以将其放在Unit3.cpp
#pragma hdrstop
之前。
您应该在刚刚显示的消息之前收到其他错误/警告消息,提醒您String
未声明。在解决后来的问题之前,请始终先解决您收到的第一个错误/警告,因为它们可能是从第一个错误中级联出来的虚假消息。