-4

So I recently decided to pick up programming again and went with C++. Tried to make an adventurer class, but I seem to be running into some trouble. Here are my files:

Adventurer.h:

#ifndef __Adventurer_H_INCLUDED__   //if Adventurer.h hasn't been included yet...
#define __Adventurer_H_INCLUDED__   //#define this so the compiler knows it has been included

class Adventurer
{

    private:

        int hp, mp, str, agi, magic, armour;

    public:

        Adventurer(){}
        void printStats();

}

#endif

Adventurer.cpp:

#include <iostream>
#include "Adventurer.h"



Adventurer::Adventurer()
{

    hp = 50;
    mp = 25;
    str = 5;
    agi = 5;
    magic = 5;
    armour = 5;
}

void Adventurer::printStats()
{

    cout << "HP = " << hp << "\n\n";
    cout << "MP = " << mp << "\n\n";
    cout << "str = " << str << "\n\n";
    cout << "agi = " << agi << "\n\n";
    cout << "magic = " << magic << "\n\n";
    cout << "armour = " << armour << "\n\n";
}

RPG_Game.cpp:

// my first program in C++
#include <iostream>
#include <string>
#include "Adventurer.h"

;using namespace std;

int main()
{
    cout << "Hello Adventurer! What is your name? \n";
    string advName;
    cin >> advName;
    cout << "\nYour name is " << advName << "!";
    Adventurer *adv = new Adventurer();
    cout << adv.printStats();
    delete adv;
    system(pause);
}
  • 1
    Pick up a [Good book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and read it, this is a rather basic mistake - you're using the wrong operator on a pointer. – The Forest And The Trees Mar 30 '15 at 07:37

1 Answers1

0

Let's look at the errors in your code

First, in your Adventurer.h, put a semicolon (;) after the class.

Next, in that same class, you have

Adventurer(){}

change this to

Adventurer();

Then, in your RPG_Game.cpp , change

cout << adv.printStats();

to

adv->printStats() ;

When using pointers, you need to use -> and not .

And lastly,

system(pause);

should be

system( "pause" );

Now, try running your code.

Also, you might find this helpful.

Community
  • 1
  • 1
Arun A S
  • 6,421
  • 4
  • 29
  • 43
  • Thank you very very much. As you can tell still very new and a lot of java instead of C++ language. Thank you for correcting my code and helping me out. I may not know the exact reasons for why whats being done is being done but at least now I know certain areas I can look at to further gain a grasp. Again thank you! – Aaron Slama Mar 30 '15 at 10:21