c++ - Overloading default constructor causing error -



c++ - Overloading default constructor causing error -

this question has reply here:

what undefined reference/unresolved external symbol error , how prepare it? 23 answers

i've been developing c# , java (among many other languages) many years, , i'm getting feet wet in c++. i've buried myself in resources on inheritance , classes, can't seem figure out how rid of pesky error.

my code:

entity.h:

#ifndef entity_h #define entity_h #include <iostream> class entity { public: std::string m_name; void update(float delta); entity(); private: virtual void step(float delta); }; #endif

entity.cpp:

#include "entity.h" entity::entity() { m_name = "generic entity"; } void entity::update(float delta) { step(delta); } void entity::step(float delta) {}

player.h:

#ifndef player_h #define player_h #include "entity.h" class player : public entity { public: player(); private: virtual void step(float delta); virtual void draw() const; }; #endif

player.cpp:

#include "player.h" player::player() { m_name = "player"; } void player::step(float delta) {} void player::draw() const {}

main.cpp:

int main() { homecoming 0; }

as can see, i'm not doing classes, i'm getting these errors:

error 3 error lnk1120: 1 unresolved externals c:\[...]\debug\consoleapplication1.exe consoleapplication11 error 2 error lnk2019: unresolved external symbol "public: __thiscall entity::entity(void)" (??0entity@@qae@xz) referenced in function "public: __thiscall player::player(void)" (??0player@@qae@xz) c:\[...]\consoleapplication1\player.obj consoleapplication1

update: code magically works when comment out next code in player.cpp:

/*player::player() { m_name = "player"; }*/

it looks entity isn't linked player. create sure output shows compiling , both added project

you need define virtual destructor in base of operations class

edit:

it doesn't have compile first, mistake. in c/c++, programme creation in 2 steps. first compiler creates obj files cpp files, such entity.obj, player.obj , on. linker bundle together.

in player.cpp, have class named entity @ point, compiler finds definition of class in .h file. player.cpp gets transformed executable code in player.obj won't contain entity.obj executable code. compilation step works.

then linker seek parse player.obj , find entity.obj compiler said exists. if doesn't "undefined reference" error, because definitions found not match actual executable.

the virtual destructor mandatory. way inheritance works in c++ virtual table. each class inheritance, virtual table (vtable) created virtual entries. when linking step performed, linker fill vtable actual function. when code executes, checks vtable class execute function. allows "override" base of operations methods or utilize base of operations method if nil new added. virtual destructor creates entry in table destructor. if there no entry virtual destructor, kid class not have entry , won't able destroy base of operations class properly, results in undefined behavior

c++ inheritance

Comments

Popular posts from this blog

assembly - What is the addressing mode for ld, add, and rjmp instructions? -

vowpalwabbit - Interpreting Vowpal Wabbit results: Why are some lines appended by "h"? -

Is there a way to convert an HTML page styled with Bootstrap CSS into email-compatible html? -