Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

calling this method.

richasrrichasr Member Posts: 4
In C++ Builder I've created the following method in Unit2:

void __fastcall TStackBuild::StackA(void)
{
int a = 1;
while (a > 0)
{
TShape* tokena;
tokena = new TShape(this);
tokena->Parent = frmNim->pnlA;
tokena->Shape = stRectangle;
tokena->Brush->Color = clWhite;
tokena->Width = 20;
tokena->Height = 20;
tokena->Top = 15;
tokena->Left = -10 + (22 * frmNim->pnlA->ControlCount);

a--;
}
}

How would I go about calling that method in Unit1? which is my main method. Considering the Call function is not used in this program.

Comments

  • linardilinardi Member Posts: 6
    Have you tried Unit1->StackA(); ?
  • richasrrichasr Member Posts: 4
    : Have you tried Unit1->StackA(); ?
    :

    Yes, states it cannot find StackA();

  • LobotomizerLobotomizer Member Posts: 5
    Make sure you've added the function prototype in the header file (Unit2.h) in the public section:
    [code]public: // User declarations
    void __fastcall StackA(void);
    [/code]
    Then include Unit2.h in Unit1.cpp by either manually adding:
    [code]#include "Unit2.h"[/code]
    at the top of the file, or use the menu option File->Include unit hdr...

    Then you should be able to call the function from Unit1:
    [code]StackBuild->StackA()[/code]

  • richasrrichasr Member Posts: 4
    : Make sure you've added the function prototype in the header file
    : (Unit2.h) in the public section:
    : [code]: public: // User declarations
    : void __fastcall StackA(void);
    : [/code]:
    : Then include Unit2.h in Unit1.cpp by either manually adding:
    : [code]: #include "Unit2.h"[/code]:
    : at the top of the file, or use the menu option File->Include unit
    : hdr...
    :
    : Then you should be able to call the function from Unit1:
    : [code]: StackBuild->StackA()[/code]:
    :
    :
    Thanks for the replies,I've done all that and it still won't call it.

    When I use StackBuild->StackA(); it states theres no such thing as StackBuild or 'undefined symbol' rather. Then when i put TStackBuild->StackA(); it states 'improper use of typedef __fastcall...' etc.

    That's even with the includes. Unsure of what to do next.
  • LobotomizerLobotomizer Member Posts: 5
    Ok. I assumed that StackBuild is the name of the form associated with Unit2 and that you added StackA() as a member of this form. Perhaps that's not a correct assumption, but if that is the case a code like this should work:

    Unit2.cpp:
    [code]//---------------------------------------------------------------------------

    #include
    #pragma hdrstop

    #include "Unit2.h"
    //---------------------------------------------------------------------------
    #pragma package(smart_init)
    #pragma resource "*.dfm"
    TStackBuild *StackBuild;
    //---------------------------------------------------------------------------
    __fastcall TStackBuild::TStackBuild(TComponent* Owner)
    : TForm(Owner)
    {
    }
    //---------------------------------------------------------------------------
    void __fastcall TStackBuild::StackA(void)
    {
    int a = 1;
    while (a > 0)
    {
    TShape* tokena;
    tokena = new TShape(this);
    tokena->Parent = frmNim->pnlA;
    tokena->Shape = stRectangle;
    tokena->Brush->Color = clWhite;
    tokena->Width = 20;
    tokena->Height = 20;
    tokena->Top = 15;
    tokena->Left = -10 + (22 * frmNim->pnlA->ControlCount);

    a--;
    }
    }
    //---------------------------------------------------------------------------
    [/code]
    Unit2.h:
    [code]//---------------------------------------------------------------------------

    #ifndef Unit2H
    #define Unit2H
    //---------------------------------------------------------------------------
    #include
    #include
    #include
    #include
    //---------------------------------------------------------------------------
    class TStackBuild : public TForm
    {
    __published: // IDE-managed Components
    private: // User declarations
    public: // User declarations
    __fastcall TStackBuild(TComponent* Owner);
    void __fastcall StackA(void);
    };
    //---------------------------------------------------------------------------
    extern PACKAGE TStackBuild *StackBuild;
    //---------------------------------------------------------------------------
    #endif
    [/code]
    Unit1.cpp:
    [code]//---------------------------------------------------------------------------

    #include
    #pragma hdrstop

    #include "Unit1.h"
    #include "Unit2.h"
    //---------------------------------------------------------------------------
    #pragma package(smart_init)
    #pragma resource "*.dfm"
    TForm1 *Form1;
    //---------------------------------------------------------------------------
    __fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
    {
    }
    //---------------------------------------------------------------------------

    void __fastcall TForm1::Button1Click(TObject *Sender)
    {
    StackBuild->StackA();
    }
    //---------------------------------------------------------------------------
    [/code]
    If you still can't get it to work, please post some more information and/or source code and I'm sure we'll work it out...
Sign In or Register to comment.