I have a MenuItem class declared like this
typedef byte (menuFn)(void); //does the things
typedef String (updateFn)(void); //updates the display string
class MenuItem {
public:
MenuItem(String dString, menuFn *onCkFn, updateFn *onUpdFn, bool clickable);
byte click(void);
void update(void);
String getDisplayString(void);
void setDisplayString(String);
private:
String _displayString;
bool _isClickable;
menuFn *_onClickFn;
updateFn *_onUpdateFn;
};
MenuItem constructor:
MenuItem::MenuItem(String dString, menuFn *onCkFn, updateFn *onUpdFn, bool clickable)
{
_displayString = dString;
_isClickable = clickable;
_onClickFn = onCkFn;
_onUpdateFn = onUpdFn;
}
which is then used in another class Menu, like this:
class Menu{
public:
Menu(MenuItem menuItems[4], MenuItem staticItems[2]);
void setItem(String itemText, byte num, menuFn itemClickFn, updateFn itemUpdateFn, bool itemClickable);
void display(void);
byte doItem(byte buttonPressed);
private:
MenuItem _menuItems[4];
MenuItem _staticItems[2];
};
and here is the Menu constructor:
Menu::Menu(MenuItem theMenuItems[4], MenuItem theStaticItems[2])
{
for (byte i = 0; i < 4; i++)
{
_menuItems[i] = theMenuItems[i];
}
for (byte i = 0; i < 2; i++)
{
_staticItems[i] = theStaticItems[i];
}
}
When I go to compile I get this error:
[..]\Menu.cpp: In constructor 'Menu::Menu(MenuItem)':
Menu.cpp:7: error: no matching function for call to 'MenuItem::MenuItem()'
Menu::Menu(MenuItem theMenuItems[4], MenuItem theStaticItems[2])
^
[..]\Menu.cpp:7:33: note: candidates are:
In file included from [..]\Menu.h:5:0,
from [..]\Menu.cpp:3:
[..]\MenuItem.h:11:3: note: MenuItem::MenuItem(String, byte (*)(), String (*)(), bool)
MenuItem(String dString, menuFn *onCkFn, updateFn *onUpdFn, bool clickable);
^
[..]\MenuItem.h:11:3: note: candidate expects 4 arguments, 0 provided
The error seems to point to me needing to give the parameters for the MenuItems when I declare the Menu. I don't understand how that would work, since they're not known until I make some menuItem Instances and then make a Menu instance to contain them.
This is using the Arduino IDE BTW, so it's c++ (ish).
Related
I have an application with several Forms. Two of them are quite similar, they have features in the form of VCL objects (labels, images, etc...) in common, which I named the same.
I want to have a function in a specific class which can accept one of these two Form as a parameter in order to modify the parameters that they have in common. The solution I came around does not seem to work.
As my application is quite big and complicated, I replicated the problem using a small example.
First, below is an example of my MainForm :
And an example of one subForm (they are all arranged in a similar way)
I have an additionnal class which is used to fill in the Edits on the subForms. The code for this class is the following:
#pragma hdrstop
#include "master_class.h"
#include "sub_Form2.h"
#include "sub_Form3.h"
#include "sub_Form4.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
Master::Master(void)
{
}
Master::~Master(void)
{
}
void Master::WriteToForm(TForm* Form)
{
TForm2* curForm = static_cast<TForm2*>(Form);
TForm3* self = dynamic_cast<TForm3*>(Form);
TForm2* self2 = dynamic_cast<TForm2*>(Form);
if (self != NULL && self2 == NULL) {
TForm3* curForm = static_cast<TForm3*>(Form);
}
else if (self == NULL && self2 != NULL) {
TForm2* curForm = static_cast<TForm2*>(Form);
}
curForm -> Edit1 -> Text = "blablabla_1";
curForm -> Edit2 -> Text = "blablabla_2";
}
And in the MainForm, the code for the "Fill Form2" button is the following:
Master1 -> WriteToForm(Form2);
where Master1 is just an object of the Master class.
This works very well for Form2 :
But for Form3, which is filled up using Master1 -> WriteToForm(Form3), here is what I get, which the same pb than in my real application:
So what should go to the Edit, is misplaced. I think the main pb comes from the fact that I did not create every label, edit, etc... on the same order. I did that on purpose to mimic my real application. To verify this, I created a 3rd subForm, where this time the VCL objects were created in the same order as my first subForm, and this works:
So I would suspect that this comes from the initial cast
TForm2* curForm = static_cast<TForm2*>(Form);
When I pass Form3 as an argument, Form3 is somewhat casted into the "shape" of Form2, which is not defined in the same order. Maybe this could be corrected by modifying directly the DFM file, but it is not a realistic approach for my main app.
I do this initial cast otherwise I get a compilation error saying that curForm is not known at the first line
curForm -> Edit1 -> Text = "blablabla_1";
So, is there a better way to pass the Form as an argument to the WriteToForm function?
Just because two types are similar does not mean they are related. Your code does not work because your two Form classes are not related to each other in any way. You can't just cast one to the other arbitrarily.
To solve this, you have several options:
code for both Form classes separately, eg:
void Master::WriteToForm(TForm* Form)
{
TForm2* curForm2 = dynamic_cast<TForm2*>(Form);
TForm3* curForm3 = dynamic_cast<TForm3*>(Form);
if (curForm2)
{
curForm2->Edit1->Text = _D("blablabla_1");
curForm2->Edit2->Text = _D("blablabla_2");
}
else if (curForm3)
{
curForm3->Edit1->Text = _D("blablabla_1");
curForm3->Edit2->Text = _D("blablabla_2");
}
}
Or:
void WriteToForm(TForm2* Form);
void WriteToForm(TForm3* Form);
...
void Master::WriteToForm(TForm2* Form)
{
Form->Edit1->Text = _D("blablabla_1");
Form->Edit2->Text = _D("blablabla_2");
}
void Master::WriteToForm(TForm3* Form)
{
Form->Edit1->Text = _D("blablabla_1");
Form->Edit2->Text = _D("blablabla_2");
}
Make your function use a template (however, be aware of this: Why can templates only be implemented in the header file?):
template<typename T>
void WriteToForm(T* Form);
...
void Master::WriteToForm<T>(T* Form)
{
Form->Edit1->Text = _D("blablabla_1");
Form->Edit2->Text = _D("blablabla_2");
}
make the two Form classes derive from a common base class or interface, eg:
class TBaseForm : public TForm
{
public:
inline __fastcall TBaseForm(TComponent *Owner) : TForm(Owner) {}
virtual void SetEdit1(const String &Text) = 0;
virtual void SetEdit2(const String &Text) = 0;
};
...
class TForm2 : public TBaseForm
{
...
public:
__fastcall TForm2(TComponent *Owner);
...
void SetEdit1(const String &NewText);
void SetEdit2(const String &NewText);
};
__fastcall TForm2::TForm2(TComponent *Owner)
: TBaseForm(Owner)
{
...
}
void TForm2::SetEdit1(const String &NewText)
{
Edit1->Text = NewText;
}
void TForm2::SetEdit2(const String &NewText)
{
Edit2->Text = NewText;
}
...
repeat for TForm3...
...
void Master::WriteToForm(TBaseForm* Form)
{
Form->SetEdit1(_D("blablabla_1"));
Form->SetEdit2(_D("blablabla_2"));
}
Or:
__interface INTERFACE_UUID("{E900785E-0151-480F-A33A-1F1452A431D2}")
IMyIntf : public IInterface
{
public:
virtual void SetEdit1(const String &Text) = 0;
virtual void SetEdit2(const String &Text) = 0;
};
...
class TForm2 : public TForm, public IMyIntf
{
...
public:
__fastcall TForm2(TComponent *Owner);
...
void SetEdit1(const String &NewText);
void SetEdit2(const String &NewText);
};
__fastcall TForm2::TForm2(TComponent *Owner)
: TForm(Owner)
{
...
}
void TForm2::SetEdit1(const String &NewText)
{
Edit1->Text = NewText;
}
void TForm2::SetEdit2(const String &NewText)
{
Edit2->Text = NewText;
}
...
repeat for TForm3...
...
void Master::WriteToForm(IMyIntf* Intf)
{
Intf->SetEdit1(_D("blablabla_1"));
Intf->SetEdit2(_D("blablabla_2"));
}
use RTTI to access the fields, eg:
#include <System.Rtti.hpp>
void Master::WriteToForm(TForm* Form)
{
TRttiContext Ctx;
TRttiType *FormType = Ctx.GetType(Form->ClassType());
TRttiField *Field = FormType->GetField(_D("Edit1"));
if (Field)
{
TValue value = Field->GetValue(Form);
if( (!value.Empty) && (value.IsObject()) )
{
TObject *Obj = value.AsObject();
// Either:
static_cast<TEdit*>(Obj)->Text = _D("blablabla_1");
// Or:
TRttiProperty *Prop = Ctx.GetType(Obj->ClassType())->GetProperty(_D("Text"));
if (Prop) Prop->SetValue(Obj, String(_D("blablabla_1")));
}
}
Field = FormType->GetField(_D("Edit2"));
if (Field)
{
TValue value = Field->GetValue(Form);
if( (!value.Empty) && (value.IsObject()) )
{
TObject *Obj = value.AsObject();
// Either:
static_cast<TEdit*>(Obj)->Text = _D("blablabla_2");
// Or:
TRttiProperty *Prop = Ctx.GetType(Obj->ClassType())->GetProperty(_D("Text"));
if (Prop) Prop->SetValue(Obj, String(_D("blablabla_2")));
}
}
}
I have the constructor
class MyFrame : public wxFrame { // defines the options on the top bar of the screen here we have:
public:
MyFrame();
private:
void OnHello(wxCommandEvent& event); // hello option
void OnExit(wxCommandEvent& event); // exit option
void OnAbout(wxCommandEvent& event); // about option
void OnHelp(wxCommandEvent& event); // event option
// void OnCourseTextBoxClicked(wxCommandEvent &event);
void OnOneHundredLevelDisplayGpButtonClicked(wxCommandEvent &event);
void OnTwoHundredLevelDisplayGpButtonClicked(wxCommandEvent &event);
void OnThreeHundredLevelDisplayGpButtonClicked(wxCommandEvent &event);
void OnFourHundredLevelDisplayGpButtonClicked(wxCommandEvent &event);
void OnFiveHundredLevelDisplayGpButtonClicked(wxCommandEvent &event);
void OnDisplayCgpaButtonClicked(wxCommandEvent &event);
// Common Courses equating their credit load
/* 100 LEVEL FIRST SEMESTER */
int CHM111 = 3;
int CHM113 = 3;
int MTH111 = 3;
int MTH112 = 3;
int PHY111 = 3;
int PHY113 = 3;
int GST111 = 2;
int GST112 = 2;
/* 100 LEVEL SECOND SEMESTER */
int CHM122 = 3;
int CHM124 = 3;
int MTH123 = 3;
int MTH125 = 3;
int PHY109 = 2;
int PHY124 = 4;
int GST121 = 2;
int GST122 = 2;
int GST123 = 2;
int LEVEL_TOTAL_100 = 47;
}
And somewhere else in the code base, some text is entered into a text box. The text box is supposed to contain one of the variables defined in the constructor which evaluate to their respective integers.
When the text box's content is evaluated e.g.:
course_one_text_box->GetValue();
evaluates
CHM111
I want to grab the constructor integer value for the CHM111 which is 3. I want to employ this value in a mathematical formula.
How do I do this?
Is there a particular syntax that grabs the constructor recorded version of the variable instead?
Thanks.
wxTextCtrl works with wxString, not with numbers.
But it's really easy to get a number from a wxString: Use wxString::ToCLong. For example:
long value;
mystring = mytextctrl->GetValue();
if ( ! mystring.ToCLong(&value) )
some message here, conversion failed (not a number)
//Your comparisons:
if ( value == (long)CHM111 )
....
So, if the user enters 3 the 'if' is true because CMH111 is initialized as 3
Notice the (long) cast from an int to avoid compiler warning. Why not use long instead of int for the type of CHMxxx?
What is not possible in C++ is to compare against the name of a variable.
#Alvindera97,
Is your program should restrict the user to enter only those constants? Then you have a wrong tool for the job!
Look at the wxComboBox/wxChoice. This way the user will have the only choice to select from the list and you won't need to keep those constants definitions in the class.
Hope this helps!
I keep getting the error:
[BCC32 Error] DogReport.cpp(29): E2288 Pointer to structure required on left side of -> or ->*
when trying to compile.
I am trying to populate a TListView with elements from my TList made up of structs.
void __fastcall TDogReportForm::FormCreate(TObject *Sender)
{
DogListView->Items->Clear();
for (int i = 0; i < DogList->Count; i++) {
TListItem * Item;
Item = DogListView->Items->Add();
Item->Caption = DogList->Items[i]->firstName;
Item->SubItems->Add(DogList->Items[i]->lastName);
Item->SubItems->Add(DogList->Items[i]->ownerName);
Item->SubItems->Add(DogList->Items[i]->hours);
Item->SubItems->Add(DogList->Items[i]->dogNum);
}
}
There is an error on each line that contains DogList->
TList holds untyped void* pointers. Its Items[] property getter returns a void* pointer. You need to type-cast it in order to access your data fields:
// DO NOT use the OnCreate event in C++! Use the actual constructor instead...
__fastcall TDogReportForm::TDogReportForm(TComponent *Owner)
: TForm(Owner)
{
DogListView->Items->Clear();
for (int i = 0; i < DogList->Count; i++)
{
// use whatever your real type name is...
MyDogInfo *Dog = static_cast<MyDogInfo*>(DogList->Items[i]); // <-- type-cast needed!
TListItem *Item = DogListView->Items->Add();
Item->Caption = Dog->firstName;
Item->SubItems->Add(Dog->lastName);
Item->SubItems->Add(Dog->ownerName);
Item->SubItems->Add(Dog->hours);
Item->SubItems->Add(Dog->dogNum);
}
}
On a side note, instead of copying all of the dog information to the TListView, you might consider using the TListView in virtual mode (set OwnerData to true and assign an OnData event handler) so it can display the information directly from DogList on-demand when needed:
__fastcall TDogReportForm::TDogReportForm(TComponent *Owner)
: TForm(Owner)
{
DogListView->Items->Count = DogList->Count;
}
void __fastcall TDogReportForm::DogListViewData(TObject *Sender, TListItem *Item)
{
// use whatever your real type name is...
MyDogInfo *Dog = static_cast<MyDogInfo*>(DogList->Items[Item->Index]);
Item->Caption = Dog->firstName;
Item->SubItems->Add(Dog->lastName);
Item->SubItems->Add(Dog->ownerName);
Item->SubItems->Add(Dog->hours);
Item->SubItems->Add(Dog->dogNum);
}
With that said, you should change DogList to use a different container that is more type-safe then TList, such as std::vector:
std::vector<MyDogInfo> DogList;
...
MyDogInfo &Dog = DogList[index]; // <-- no type-cast needed
Item->Caption = Dog.firstName;
Item->SubItems->Add(Dog.lastName);
Item->SubItems->Add(Dog.ownerName);
Item->SubItems->Add(Dog.hours);
Item->SubItems->Add(Dog.dogNum);
I would like to send back data to class that create this object.
It's game related.
The enemy objects have a threaded function and move on their own in the scene.
It generates lots of errors if you include the header file from the class that creates to the objects into the object itself ... to pass pointers.
Enemy Class:
Class Enemy
{
private:
void (*iChange)(DWORD &);
}:
Enemy::Enemy(void (*iChangeHandler)(DWORD &) ) : iChange(0)
{
this->iChange = iChangeHandler;
}
void Enemy::Draw(D3DGraphics& gfx)
{
this->iChange(this->dwThreadID); // send a message back to the class that created me
gfx.PutPixel(this->my_position_x + 0,this->my_position_y,this->red,this->blue,this->green);
this->grafix->DrawCircle(this->my_position_x + 0,this->my_position_y, this->radius, this->red,this->blue,this->green);
(sprintf)( this->enemy_buffer, "X: %d, Y: %d", this->my_position_x , this->my_position_y);
this->grafix->DrawString( this->enemy_buffer, this->my_position_x , this->my_position_y, &fixedSys, D3DCOLOR_XRGB(255, 0, 0) );
}
Game Class:
struct enemies_array_ARRAY {
std::string name;
DWORD ID;
Enemy* enemy;
} enemies_array[25];
void Game::EnemyEvent(DWORD &thread_id)
{
enemies_array[0]...... // i want to acces this struct array
}
Game::Game(HWND hWnd)
{
enemies_array[0].name = "john Doe";
enemies_array[0].ID = NULL;
enemies_array[0].enemy = new Enemy(&Game::EnemyEvent);
// error: C2664:
// another attemp
enemies_array[0].name = "john Doe";
enemies_array[0].ID = NULL;
enemies_array[0].enemy = new Enemy(Game::EnemyEvent);
// error C3867:
}
If I understand correctly, you want to call a function on the Game object. This means you need to pass a pointer to the Game object in order to correctly call a non static member function pointer(iChange) on it.
Make the changes shown below and you should be able to do what you want
enemies_array[0].enemy = new Enemy(this,&Game::EnemyEvent);
typedef void (Game::*ChangeFunc)(DWORD &)
Class Enemy
{
private:
ChangeFunc iChange;
Game *pGame;
}:
Enemy(Game *pCreatorGame, ChangeFunc iChangeHandler )
{
iChange = iChangeHandler;
pGame = pCreatorGame;
}
void Enemy::Draw(D3DGraphics& gfx)
{
(pGame->*iChange)(this->dwThreadID);
I am trying to compile a custom class in Xcode 3 and I keep getting a no matching function call error although the same custom class compiles fine under Windows. Obviously something is not right regarding the use of curly brackets and the XCode compiler. The compiler is choking at the first curly bracket { below:
: ADataBrowser(inOwnerWindow,inID,inOwner), mEncoding(kTextEncodingMacRoman)
{
std::memset( mCustomLabels, 0, sizeof(CFStringRef) * kMaxLevelCount);
}
Any ideas much appreciated!
//Full source below
#pragma once
#include <ADataBrowser.h>
#include <AControls.h>
enum
{
kMaxLevelCount = 16
};
class CArray;
class ACustomLabelList :
public ADataBrowser
{
public:
ACustomLabelList(
ControlRef inControl,
bool inOwner = false)
: ADataBrowser(inControl, inOwner), mEncoding(kTextEncodingMacRoman)
{
std::memset( mCustomLabels, 0, sizeof(CFStringRef) * kMaxLevelCount);
}
ACustomLabelList(
WindowRef inOwnerWindow,
const ControlID &inID,
bool inOwner = false)
: ADataBrowser(inOwnerWindow,inID,inOwner), mEncoding(kTextEncodingMacRoman)
{
std::memset( mCustomLabels, 0, sizeof(CFStringRef) * kMaxLevelCount);
}
ACustomLabelList(
WindowRef inOwnerWindow,
const Rect &inBounds,
DataBrowserViewStyle inStyle)
: ADataBrowser(inOwnerWindow, inBounds, inStyle), mEncoding(kTextEncodingMacRoman)
{
std::memset( mCustomLabels, 0, sizeof(CFStringRef) * kMaxLevelCount);
}
virtual ~ACustomLabelList();
void Initialize(CArray *inArray, const TextStyle &inStyle);
CFStringRef GetCurrentSelectionLabelString();
void SetCurrentSelectionLabelString(CFStringRef inString);
void SetLabelStringAt(CFStringRef inString, DataBrowserItemID inRowID);
void ShiftCurrentSelectionUp();
void ShiftCurrentSelectionDown();
void SendSelectionChangedEvent();
CFStringRef * GetLabelList() { return mCustomLabels; }
void GetLabelAt(Str15 outString, UInt32 inIndex);
protected:
virtual void ItemNotification(
Item &inItem,
DataBrowserItemNotification inMessage,
ItemData &inItemData);
virtual OSStatus GetItemData(
Item &inItem,
DataBrowserPropertyID inProperty,
ItemData &inItemData);
virtual OSStatus SetItemData(
Item &inItem,
DataBrowserPropertyID inProperty,
ItemData &inItemData);
CFStringRef mCustomLabels[kMaxLevelCount];
TextEncoding mEncoding;
private:
ACustomLabelList(const ACustomLabelList&);
ACustomLabelList& operator=(const ACustomLabelList&);
};
It looks like you're not including the file that declares std::memset.