Weiteres Problem mit dynamic_cast
-
Hallo,
habe ein Problem.TComponent *comp = pForm->Components[i]; ... else if (pForm->Components[i]->ClassNameIs("TStringGrid")) { TStringGrid * pStringGridGrid = dynamic_cast<TStringGrid *>(comp); ... }Bekomme dabei eine Fehlermeldung. TStringGrid sei ein undefiniertes Symbol.
-
Schau mal in der Doku nach, ob du eventuell den richtigen Header vergessen hast.
-
also das funktioniert bei mir (nix spezielles includes o.ä)
for(int i = 0; i< Form1->ComponentCount; ++i) { if(Form1->Components[i]->ClassNameIs("TLabel")) { dynamic_cast<TLabel*>(Form1->Components[i])->Caption="A Label"; } if(Form1->Components[i]->ClassNameIs("TButton")) { dynamic_cast<TButton*>(Form1->Components[i])->Caption="A Button"; if(dynamic_cast<TButton*>(Form1->Components[i])->Name=="Button1") { dynamic_cast<TButton*>(Form1->Components[i])->Caption="Special Button"; } } if(Form1->Components[i]->ClassNameIs("TStringGrid")) { dynamic_cast<TStringGrid*>(Form1->Components[i])->Cells[1][1]="TEST"; } } }
-
Hallo,
Der Header für TStringGrid heißt übrigend grids.hpp
@ser1al
Wenn du schon dynamic_cast verwendest, solltest du seine Eigenschaften auch nutzen. Wenn du sicher bist welche Klasse hinter der Komponente steckt reicht nämlich auch ein static_cast. Ansonsten gefällt mir das hier besser (ist auch schneller
)for(int i = 0; i< Form1->ComponentCount; ++i) { TLabel* label = dynamic_cast<TLabel*>(Form1->Components[i]); if(label) { label->Caption="A Label"; } TButton* button = dynamic_cast<TButton*>(Form1->Components[i]); if(button) { button->Caption="A Button"; if(button->Name=="Button1") { button->Caption="Special Button"; } } TStringGrid* grid = dynamic_cast<TStringGrid*>(Form1->Components[i]); if(grid) { grid->Cells[1][1]="TEST"; } } }
-
Braunstein schrieb:
Hallo,
@ser1al
Wenn du schon dynamic_cast verwendest, solltest du seine Eigenschaften auch nutzen. Wenn du sicher bist welche Klasse hinter der Komponente steckt reicht nämlich auch ein static_cast.du hast natürlich recht, wollte es ja nur schnell zeigen, ohne auf sinn und unsinn einzugehen!
-
Danke für eure Tips. Habe es hinbekommen.