Looking at RAD Studio 11.1.5 and wrote a small C++ Builder test program using a thread. I keep getting compiler errors as follows:
// Execute routine of the TestThread
void __fastcall TTestThread::Execute()
{
int i,j,k;
bool MatrixOk;
long double Answer, ReadValue, SubDeterminant;
const HRESULT iniResult = CoInitializeEx(0, COINIT_MULTITHREADED);
if (!((iniResult == S_OK) || (iniResult == S_FALSE))) {
Application->MessageBox(L"Failed to initialize COM library.\n",L"Thread Error", MB_OK);
return;
}
// Place main part of thread here
while(!Terminated) {
Synchronize(UpdateDisplay);
Sleep(1000);
}
TheMessage = "Terminating... Cya";
Synchronize(ShowMessage);
CoUninitialize();
return;
}
void __fastcall TTestThread::UpdateDisplay()
{
Counter++;
MainForm->SB->SimpleText = "Counter: "+IntToStr(Counter);
MainForm->SB->Repaint();
return;
}
void __fastcall TMainModelThread::ShowMessage()
{
Application->MessageBox(TheMessage.w_str(),L"A Message", MB_OK);
return;
}
// Code that generates the errors below called from the main form OnClose event
MainModelThread->Terminate();
Compiler error messages as follows:
[bcc64 Error] MainFrm.cpp(90): 'Terminate' is a private member of 'System::Classes::TThread'
MainFrm.h(17): constrained by implicitly private inheritance here
System.Classes.hpp(2726): member is declared here
[bcc64 Error] MainFrm.cpp(90): cannot cast 'TTestThread' to its private base class 'System::Classes::TThread'
MainFrm.h(17): implicitly declared private here
Is anyone else noticing this? This code would compile under C++ Builder 10.3.1.
You did not show the declaration of the
TTestThreadclass, but the error messages are clearly saying that you are inheriting fromTThreadusing (implicitly) private inheritance, making all members ofTThreadprivate and thus inaccessible to your class. You must use public inheritance instead, eg:Also, just FYI,
Application->MessageBox()is not thread-safe, so it must be synchronized. Your 1st call to it is not synchronized, but your 2nd call is.