Help trying to reference class variable in function

Hello. I'm trying to make a text based rpg, and I'm trying to create this function in the player.cpp file

1
2
3
void equipItem(weapon weaponVar) {
player.equippedWeapon = &weaponVar;
}


but I get this error:
 
error: expected unqualified-id before '.' token



player.h file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef PLAYER_H
#define PLAYER_H
#include <weapon.h>

class player
{
    public:
        player();
        void equipWeapon(weapon weaponVar);
        weapon* equippedWeapon;

    protected:
    private:

};

#endif // PLAYER_H 


What am I doing wrong?
1
2
3
4
5
6
void player::equipItem(weapon weaponVar) { //scope resolution operator
//player.equippedWeapon = &weaponVar;  //can't have player here

equippedWeapon = &weaponVar; // member functions have access to member variables

}


equippedWeapon should be private:

Hope all goes well !!
Eh, I'd argue for equippedWeapon being in either the private or protected section. But that really all depends on how complex you want to make your RPG. Is it only the player that knows what his weapon is? Or are there friendly classes that can see it as well, but maybe not modify it?

Also, that should be #include "weapon.h"
Last edited on
@YFGHNG

Yes, the OP's class does lack an interface of public functions. Well, the whole thing is very minimal at this stage, I gather the OP is starting out small, which is a good thing :+)

But protected variables are supposed to be a bad idea - even though they seem to be the easiest thing, they are just as bad as public variables.
Topic archived. No new replies allowed.