C++ Borland Builder forms - calling a function - c++

New to C++ so sorry if this is a basic question! I am used to Java (oh yess! so easy).
My function below addMessages is called from another file, it will then actually run __fastcall TfrmRunning::Add(). As i could not get this working from the other file. the add is part of the TdrmRunning object)
How do I get the add messages to call the Add function?
This is from Running.cpp
void __fastcall TfrmRunning::Add()
{
lbMessages->Items->Add("Application Started at ");
}
//This is called from another file as i could not get the above function working
void addMessages(){
TfrmRunning::Add(); // this does not work
}
My Header file (Running.H)
class TfrmRunning : public TForm
{
__published: // IDE-managed Components
TImage *imgLogo;
TLabel *lblCopyRight;
TLabel *lblTitle;
TButton *btnExit;
TButton *btnViewType;
TListBox *lbMessages;
void __fastcall btnExitClick(TObject *Sender);
void __fastcall FormCreate(TObject *Sender);
void __fastcall Add();
private: // User declarations
public: // User declarations
__fastcall TfrmRunning(TComponent* Owner);
};
void addMessages();

Add() isn't a static function of TfrmRunning.
You'll need an object of type TfrmRunning to invoke it.

Try using
TObjetct *asd;
Add(asd);

Related

Unreal 4: Unresolved external symbols

I just created a project in Unreal 4 with C++ (Visual Studio 2019).
As soon as I build the project with only a basic movement class, Visual Studio shows these errors (see the image).
Unresolved External Symbol error
I installed Visual studio following the official guide: https://docs.unrealengine.com/4.27/en-US/ProductionPipelines/DevelopmentSetup/VisualStudioSetup/
This is the code in the cpp file:
// Fill out your copyright notice in the Description page of Project Settings.
#include "MiJugador.h"
// Sets default values
AMiJugador::AMiJugador()
{
// Set this character to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
}
// Called when the game starts or when spawned
void AMiJugador::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void AMiJugador::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
// Called to bind functionality to input
void AMiJugador::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
check(PlayerInputComponent);
PlayerInputComponent->BindAxis("Forward", this, &AMiJugador::MoveForward);
PlayerInputComponent->BindAxis("Right", this, &AMiJugador::MoveRight);
}
void AMiJugador::MoveForward(float amount)
{
if (Controller && amount)
{
FVector fwd = GetActorForwardVector();
AddMovementInput(fwd, amount);
}
}
void AMiJugador::MoveRight(float amount)
{
if (Controller && amount)
{
FVector right = GetActorRightVector();
AddMovementInput(right , amount);
}
}
And this is the header file:
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "MiJugador.generated.h"
UCLASS()
class CPPPRACTICE_API AMiJugador : public ACharacter
{
GENERATED_BODY()
public:
// Sets default values for this character's properties
AMiJugador();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
// Called to bind functionality to input
virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
// Funciones para movimiento
void MoveForward(float amount);
void MoveRight(float amount);
};
Most of the code is automatically done by Unreal. Anyone can help me get rid of these error? Thi is geting me crazy, I've tried many things as including .lib in project settings but none of those helped me...
Thank you in advance for your time.
You are missing (or your linker can't find) an OpenGL library.
Most likely, you are including .lib in project settings wrong.

How to use Synchronize()?

The manual says that Synchronize is a member of TThread.
However it shows that you can call Synchronize directly. Other sources tell the same.
//Synchronize() performs actions contained in a routine as if they were executed from the main VCL thread
void __fastcall TCriticalThread::Execute()
{
...
Synchronize(UpdateCaption);
...
}
But if I do this, my compiler tells me "E2268 Call to undefined function 'Synchronize'". Of course I included the library:
#include <System.Classes.hpp>
On the other hand, TThread::Synchronize is found by the compiler, but it does not accept MainThreadID as parameter:
TThread::Synchronize(MainThreadID, MainForm->UpdateCaption );
PS: I am new to C++ Builder.
Synchronize() is a method of the RTL's TThread class. In all versions of C++Builder, TThread has a non-static version of Synchronize(), which is the version the code you showed is trying to call. That requires TCriticalThread to be derived for TThread, eg:
class TCriticalThread : public TThread
{
...
protected:
virtual void __fastcall Execute();
...
};
void __fastcall TCriticalThread::Execute()
{
...
Synchronize(UpdateCaption);
...
}
If that is not the case in your situation, TThread also has a static version of Synchronize() that can be used with threads that are not derived from TThread, eg:
void __fastcall TCriticalThread::Execute()
{
...
TThread::Synchronize(NULL, UpdateCaption);
...
}

BEGIN_MESSAGE_MAP caused C++ Builder 10.1 to crash to desktop

I am writing a VCL componenet, TGIcon, to mimic the Icons in windows desktop, it has been working fine until I decided to add MouseEnter and MouseLeave events to the component. I followed guides from: Embarcadero Community
and here is my code (header):
class PACKAGE TGIcon : public TGraphicControl
{
private:
AnsiString FCaption;
TPngImage *FIcon, *FDIcon;
TFont *FFont;
TNotifyEvent FOnMouseEnter;
TNotifyEvent FOnMouseLeave;
void __fastcall CMMouseEnter(TMessage &Message);
void __fastcall CMMouseLeave(TMessage &Message);
BEGIN_MESSAGE_MAP
MESSAGE_HANDLER(CM_MOUSEENTER, TMessage, CMMouseEnter)
MESSAGE_HANDLER(CM_MOUSELEAVE, TMessage, CMMouseLeave)
END_MESSAGE_MAP(TGIcon)
protected:
virtual void __fastcall Paint();
void __fastcall SetCaption(AnsiString value);
void __fastcall SetIcon(TPngImage *value);
void __fastcall SetFont(TFont *value);
public:
__fastcall TGIcon(TComponent* Owner);
__fastcall ~TGIcon();
void __fastcall MakeGray(void);
__published:
__property AnsiString Caption = {read=FCaption, write=SetCaption, nodefault};
__property TPngImage *Icon = {read=FIcon, write=SetIcon};
__property TFont *Font = {read=FFont, write=SetFont};
__property Parent;
__property Enabled;
__property OnClick;
__property TNotifyEvent OnMouseEnter = {read=FOnMouseEnter, write=FOnMouseEnter};
__property TNotifyEvent OnMouseLeave = {read=FOnMouseLeave, write=FOnMouseLeave};
};
Whenever I try to place the component on a Form, the IDE (C++ Builder Starter) would crash to desktop. I have traced the source of problem to be the "BEGIN_MESSAGE_MAP...END_MESSAGE_MAP" part. If I comment out that part, the component works fine.
I used to have the same component working in C++Builder XE5 (Professional) but since that's owned by a company I no longer work with, I don't have the binary of the component, so I have to re-write it here. As far as I can remember, what I did is exactly the same as the one I wrote in XE5, that one works but this one would crash the IDE, no error message, no Access Violation, just plain CTD.
Can someone please help, is there anything I need to do to make this work in C++ Builder 10.1 (Berlin) Starter Edition? Is this a bug of C++Builder or is this what cannot be done in Starter Edition, that it only can be done in the 'paid' editions?? Or is this method already obsolete? If so please show me how the "modernized" C++ Builder do it.
Thanks in advance.
Your MESSAGE_MAP is terminated incorrectly. In the END_MESSAGE_MAP macro, you must specify the base class that your component derives from (TGraphicControl).
A MESSAGE_MAP is just a fancy way to override the virtual Dispatch() method, where:
BEGIN_MESSAGE_MAP declares and opens the overridden method, and opens a switch statement
MESSAGE_HANDLER (use VCL_MESSAGE_HANDLER instead if your project uses ATL) declares case statements for the switch
END_MESSAGE_MAP calls the Dispatch() method of the specified class for unhandled messages, closes the switch, and closes the overridden method.
Here are the declarations from sysmac.h:
#define BEGIN_MESSAGE_MAP virtual void __fastcall Dispatch(void *Message) \
{ \
switch (((PMessage)Message)->Msg) \
{
#define VCL_MESSAGE_HANDLER(msg,type,meth) \
case msg: \
meth(*((type *)Message)); \
break;
// NOTE: ATL defines a MESSAGE_HANDLER macro which conflicts with VCL's macro. The
// VCL macro has been renamed to VCL_MESSAGE_HANDLER. If you are not using ATL,
// MESSAGE_HANDLER is defined as in previous versions of BCB.
//
#if !defined(USING_ATL) && !defined(USING_ATLVCL) && !defined(INC_ATL_HEADERS)
#define MESSAGE_HANDLER VCL_MESSAGE_HANDLER
#endif // ATL_COMPAT
#define END_MESSAGE_MAP(base) default: \
base::Dispatch(Message); \
break; \
} \
}
So, this code:
BEGIN_MESSAGE_MAP
MESSAGE_HANDLER(CM_MOUSEENTER, TMessage, CMMouseEnter)
MESSAGE_HANDLER(CM_MOUSELEAVE, TMessage, CMMouseLeave)
END_MESSAGE_MAP(TGIcon) // <-- error!
Gets translated by the preprocessor to this code, which is what the compiler sees:
virtual void __fastcall Dispatch(void *Message)
{
switch (((PMessage)Message)->Msg)
{
case CM_MOUSEENTER:
CMMouseEnter(*((TMessage *)Message));
break;
case CM_MOUSELEAVE:
CMMouseLeave(*((TMessage *)Message));
break;
default:
TGIcon::Dispatch(Message); // <-- recursive loop!
break;
}
}
As you can see, since you are specifying your own component class (TGIcon) instead of the base class (TGraphicControl) in END_MESSAGE_MAP, you are creating an endless recursion loop when the component receives an unhandled message. TGIcon::Dispatch() is calling TGIcon::Dispatch() again. It needs to call TGraphicControl::Dispatch() instead (so do your CMMouseEnter() and CMMouseLeave() methods):
BEGIN_MESSAGE_MAP
MESSAGE_HANDLER(CM_MOUSEENTER, TMessage, CMMouseEnter)
MESSAGE_HANDLER(CM_MOUSELEAVE, TMessage, CMMouseLeave)
END_MESSAGE_MAP(TGraphicControl) // <-- fixed!

Where to initialize child controls on TCustomControl

I'm trying to create a simple custom control using Borland C++ Builder 6. In this case, I am trying to create a TPageControl with a single TTabSheet on it. I am having trouble figuring out the proper place to initialize these child controls. At the moment I am initializing everything in the constructor. Everything compiles fine, but when I attempt to place the control onto a form, the Borland IDE gives me an error "Control '' has no parent window" or something very similar. I've found out that the line that is causing this specifically is the setting of the TTabSheet's PageControl property.
The code for my control is below:
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "TestControl.h"
#pragma package(smart_init)
//---------------------------------------------------------------------------
// ValidCtrCheck is used to assure that the components created do not have
// any pure virtual functions.
//
static inline void ValidCtrCheck(TTestControl *)
{
new TTestControl(NULL);
}
//---------------------------------------------------------------------------
__fastcall TTestControl::TTestControl(TComponent* Owner)
: TCustomControl(Owner)
{
pageControl = new TPageControl(this);
pageControl->Parent = this;
tabSheet = new TTabSheet(pageControl);
tabSheet->Parent = pageControl;
tabSheet->Caption = "Page 1";
tabSheet->PageControl = pageControl;
}
//---------------------------------------------------------------------------
__fastcall TTestControl::~TTestControl()
{
pageControl->Free();
}
//---------------------------------------------------------------------------
namespace Testcontrol
{
void __fastcall PACKAGE Register()
{
TComponentClass classes[1] = {__classid(TTestControl)};
RegisterComponents("Test", classes, 0);
}
}
//---------------------------------------------------------------------------
Any assistance would be much appreciated--I'm finding that due to the age of this particular technology I'm not having much luck finding resources on this.

"Using" is not solving the "hides virtual function" warning

I have been googling high and low and can't find a solution that will remove the warning, even when I use the using directive.
class TShowException_Form : public TForm {
__published: // IDE-managed Components
TButton *Send_Button;
TButton *Cancel_Button;
TLabel *Message_Label;
private: // User declarations
using TCustomForm::ShowModal;
//using TForm::ShowModal;
public: // User declarations
__fastcall TShowException_Form(TComponent* Owner);
int __fastcall ShowModal(System::Sysutils::Exception *E);
};
I want to hide the original virtual int __fastcall ShowModal(void) and expose a new one taking an Exception parameter.
But it still complaints on "hides virtual function":
[bcc32 Warning] TShowExceptionForm.h(32): '_fastcall TShowException_Form::ShowModal(Exception *)' hides virtual function '_fastcall TCustomForm::ShowModal()'
I have also tried using TForm::ShowModal; but with the same result.
Any ideas of how to solve this warning?
EDIT
I found out that it works perfectly well if I override the show() method instead:
class TShowException_Form : public TForm {
__published: // IDE-managed Components
TButton *Send_Button;
TButton *Cancel_Button;
TLabel *Message_Label;
private: // User declarations
using TForm::ShowModal;
using TForm::Show;
public: // User declarations
__fastcall TShowException_Form(TComponent* Owner);
int __fastcall Show(System::Sysutils::Exception *E);
};
So why isn't it working with ShowModal()?
bcc32 is, in many respects, not very compliant with the C++ standard. Whenever I find myself asking, "Why does this technique that I think should work in C++ not work in bcc32?", I usually assume it's yet another compiler bug.
The fact that Show works while ShowModal doesn't is interesting. Looking at Vcl.Forms.hpp shows the difference: Show is defined with HIDESBASE (a macro that expands to __declspec(hidesbase)).
Adding HIDESBASE to your ShowModal should work as well. You may also have to declare a virtual destructor, if you don't already have one, due to bcc32 compiler weirdness.
virtual __Fastcall ~TShowException_Form() {}
You must declare overloaded version as virtual too:
virtual int __fastcall ShowModal(System::SysUtils::Exception * E);
Does not know if C++Builder supports C++11, but if it does, try also to delete overload which you want to hide:
virtual int __fastcall ShowModal() = delete;
instead of placing it into private section.
You get the warning because what you are trying to do happens very often by mistake and is a serious and very hard to find bug if it is done by mistake. Maybe you should use a different name.