-1

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?

user1739825
  • 820
  • 4
  • 10
  • 27
  • The error messages shows an error on line 14 of `Unit3.cpp`, however you only pasted 12 lines of Unit3.cpp. Please double check that you have posted your exact code. – M.M Jun 29 '15 at 05:37

1 Answers1

0

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.

M.M
  • 138,810
  • 21
  • 208
  • 365
  • @user1739825 `extern` is redundant but not wrong (it's expressive so I tend to use it). Also, please, read the previous answer: declaring the 'extern' function in the `.cpp file` was given as an option (especially as a way to introduce the external linkage / translation unit concepts), not as an additional requirement. – manlio Jun 29 '15 at 09:18
  • @manlio what previous answer? I guess you mean [this](http://stackoverflow.com/questions/31067798/function-return-value-from-form-unit-and-non-form-unit) from OP's post history – M.M Jun 29 '15 at 10:36
  • @manlio I'm not suggesting `extern` is wrong; but based on OP's question it seemed he may have thought that there was a difference between using it and not using it. – M.M Jun 29 '15 at 10:41
  • Yes, that answer. Thank you for the link. – manlio Jun 29 '15 at 13:27
  • That meant either extern functions or header files can be used. I have noted that while header files are needed to make references to the forms or components, extern functions or header files can be used to make references to functions in formless units. Correct? – user1739825 Jun 30 '15 at 03:40
  • @user1739825 only function declarations may be used to make references to functions in other units. There is no difference between "unit" and "formless unit"; and `extern` in the function declaration makes no difference either. The "difference" here is whether you put the function declaration in the header file or not. It is a bad idea to put it anywhere other than the header file corresponding to the `.cpp` file containing the function body. – M.M Jun 30 '15 at 04:21