tBane Temat założony przez niniejszego użytkownika |
[c++] Konstruktor w dziedziczeniu » 2023-11-14 19:10:32 Witam. Mam dwie klasy button oraz textButton. Obie klasy sa bardzo podobne, z tym że ta druga posiada rownież tekst, tak więc klasa textButton dziedziczy po button. Chcialbym, aby konstruktor klasy textButton wykonał to co konstruktor klasy bazowej plus np. prxypisał tekst. Jak napisać taki konstruktor? class button { public: float x, y, w, h; float r, g, b, a; bool pressed; float pr, pg, pb, pa; button * next; button( float, float, float, float ); ~button(); void setColor( float, float, float, float ); void setPressedColor( float, float, float, float ); bool press( float, float ); };
class textButton : public button { public: text * t; textButton( float, float, float, float, string ); ~textButton(); virtual void render(); };
button::button( float x, float y, float width, float height ) { this->x = x; this->y = y; this->w = width; this->h = height; r = 64.0f; g = 64.0f; b = 64.0f; a = 256.0f; pressed = false; pr = 80.0f; pg = 80.0f; pb = 80.0f; pa = 256.0f; next = NULL; }
textButton::textButton( float x, float y, float width, float height, string txt ) : button( x, y, width, height ) { }
|
|
pekfos |
» 2023-11-14 23:05:27 A co było nie tak z kodem który podałeś? Sam sobie odpowiedziałeś. |
|
tBane Temat założony przez niniejszego użytkownika |
» 2023-11-15 16:08:29 Otóż dostaje taki komunikat /data/user/0/ru.iiec.cxxdroid/files/bin/ld: /data/user/0/ru.iiec.cxxdroid/cache/iiec_tmp_source_file-15d357.o: in function `textButton::textButton(float, float, float, float)': iiec_tmp_source_file.txt:(.text+0x19c8): undefined reference to `vtable for textButton' /data/user/0/ru.iiec.cxxdroid/files/bin/ld: iiec_tmp_source_file.txt:(.text+0x19cc): undefined reference to `vtable for textButton' clang-9: error: linker command failed with exit code 1 (use -v to see invocation)
|
|
pekfos |
» 2023-11-15 18:32:00 Obstawiam że nie masz implementacji dla textButton::render(). To jedyna wirtualna metoda w tym kodzie, więc po prostu ją usuń. Albo ją zaimplementuj, ale wyraźnie nie jest używana do niczego. |
|
tBane Temat założony przez niniejszego użytkownika |
» 2023-11-15 18:46:10 Miałeś rację pekfos. Zakomentowalem metodę render(), i zadziałało. Dziwny błąd.. |
|
pekfos |
» 2023-11-15 19:24:03 Vtable zawiera adresy wszystkich metod wirtualnych w klasie. Jak nie masz w pliku implementacji żadnej metody wirtualnej, to kompilator zakłada że vtable też jest zdefiniowane w innym pliku, stąd ten błąd. Jakby brakowało implementacji do 1 z N>1 metod to by było undefined reference do konkretnej metody. |
|
tBane Temat założony przez niniejszego użytkownika |
» 2023-11-15 19:50:27 |
|
« 1 » |