0

I have looked for the solution to the problem below all over the internet. Some of the solutions given are exactly what I am doing, yet I am still getting an error when I try to compile my code. Any help would be greatly appreciated.

I have a base class Stats that every monster class in my code will be derived from. Here is the 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 I have a monster class (here Orc), that is derived from the Stats class. The constructor of the monster class also calls the overloaded constructor of the Stats class:

#include <iostream>
#include "Stats.cpp"

class Orc: public Stats {
    public:
        Orc();
};

Orc::Orc() : Stats(8, 3, 1) {}

In the main function I build a new Orc object and try to call the base class function Stats::getArm() with the object:

int main() {
    Orc mork();
    std::cout << "Armor: " << mork.Stats::getArm() << "\n";
}

I expect to have the function return the int value for armor. Instead I am getting the error:

error: request for member 'Stats:: getArm' in 'mork', which is of a non-class type 'Orc()'

By the way, I am compiling in c++11.

M.M
  • 138,810
  • 21
  • 208
  • 365
Carlos
  • 41
  • 1
  • 1
  • 2
  • You are not defining the instantiation properly. In the main, you have to do Orc mork; and then call mork.getArm(); You probably want to make getArm virtual. – Javi Jul 07 '15 at 22:42
  • 1
    Possible duplicate of http://stackoverflow.com/questions/1424510/most-vexing-parse-why-doesnt-a-a-work – Ediac Jul 07 '15 at 22:46
  • That works. Thank you. – Carlos Jul 07 '15 at 22:50

1 Answers1

3
Orc mork();

This line does not do what you think it does. You meant to type:

Orc mork;

Which would declare an Orc object named mork. Instead, you declared a function named mork that takes no arguments and returns an Orc.

rlbond
  • 65,341
  • 56
  • 178
  • 228
  • Oh geez. Face palm. Thank you for pointing out my stupid mistake. – Carlos Jul 07 '15 at 22:50
  • 1
    @Carlos since you are using C++11, get into the habit of using `{ }` for initialization. That would avoid the possibility of this problem occurring. – M.M Jul 07 '15 at 23:36