Function return value from formless unit thru form unit

182 Views Asked by At

Hi I have learnt a great deal about using functions here. Many things I have learnt are:- - Function return value from another form - Function return value from another formless unit

This time, I want functions to get the quote from formless unit THROUGH another form.

Using these methods above, I tried getting the functions to return from formless unit THROUGH another form. It seems that I was not able to get it working properly.

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

I got error "

[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

". How to fix it?

1

There are 1 best solutions below

6
On BEST ANSWER

Unit1.cpp should not have extern String getQuote(); . There is no free function of that name anyway, and you never call it.

Unit2.h and Unit2.cpp both should not have extern String FindQuote(); . Instead, Unit2.cpp should #include "Unit3.h" (which it does) so that it sees String FindQuote();.

In case you are unaware, the extern is redundant , extern X f(); is the same as X f();.


Your error is that String has not been declared. You must have #include <vcl.h> visible before any code that uses String. You could either put this at the top of Unit3.h, or put it in Unit3.cpp just before #pragma hdrstop.

You should have got other error/warning messages before the one you just showed, alerting you to the fact that String was not declared. Always address the first error/warning you get before moving onto worrying about later ones, as they can be bogus messages that cascade from the first error.