c++ - Calling a base class function from a derived class object. Base class data members set in derived class constructor -
i have looked solution problem below on internet. of solutions given doing, yet still getting error when try compile code. appreciated.
i have base class stats every monster class in code derived from. here base class:
#include <iostream> class stats { private: int hitpoints; int armor; int bonus; public: int gethp(){return hitpoints;} int getarm(){return armor;} int getbonus(){return bonus;} stats(); stats(int, int, int); }; stats::stats() { hitpoints = 0; armor = 0; bonus = 0; } stats::stats(int hp, int arm, int bon) { hitpoints = hp; armor = arm; bonus = bon; }
now have monster class (here orc), derived stats class. constructor of monster class calls overloaded constructor of stats class:
#include <iostream> #include "stats.cpp" class orc: public stats { public: orc(); }; orc::orc() : stats(8, 3, 1) {}
in main function build new orc object , try call base class function stats::getarm() object:
int main() { orc mork(); std::cout << "armor: " << mork.stats::getarm() << "\n"; }
i expect have function return int value armor. instead getting error:
error: request member 'stats:: getarm' in 'mork', of non-class type 'orc()'
by way, compiling in c++11.
orc mork();
this line not think does. meant type:
orc mork;
which declare orc
object named mork
. instead, declared function named mork
takes no arguments , returns orc
.
Comments
Post a Comment