So, I've started writing a turn based text game, and I'm having difficulty taking my random number made in 1 class, and using it with another. I got rid of a lot of errors, but I'm stuck on one.
#ifndef RESOURCES_H
#define RESOURCES_H
class Die{
public:
int roll();
};
class character{
public:
int hp();
bool state();
};
class attack{
public:
int number();
};
#endif
characters.cpp: In member function ‘int attack::number()’:
characters.cpp:25:14: error: invalid use of non-static member function ‘int Die::roll()’
25 | switch(Die::roll)
Now apparently I can't declare it to have static linkage
errors
1 2 3 4 5 6 7 8 9 10 11 12 13 14
In file included from main.cpp:2:
characters.h:9:22: error: cannot declare member function ‘staticint Die::roll()’ to have static linkage [-fpermissive]
9 | staticint Die::roll()
| ^
In file included from characters.cpp:4:
characters.h:9:22: error: cannot declare member function ‘staticint Die::roll()’ to have static linkage [-fpermissive]
9 | staticint Die::roll()
| ^
characters.cpp:8:5: error: redefinition of ‘staticint Die::roll()’
8 | int Die::roll()
| ^~~
In file included from characters.cpp:4:
characters.h:9:12: note: ‘staticint Die::roll()’ previously defined here
9 | staticint Die::roll()
Also note that for the switch case, these should be numbers 1 2 3 etc, not the characters '1', '2' etc. The number 1 and the character '1' is not the same.
Also, you don't need a switch at all. The damage can be calculated direct from the die roll.
1 2 3 4
int attack::number()
{
return damage = Die::roll() * 25;
}
It compiles well for me too, the problem I seem to be having is splitting it into a header file and a function file. I plan on continually growing the game to sharpen my c++ skills, so I'm going to have to learn. I might write main.cpp to make a character (player) and a character (troll) to fight using attack. I think this one's solved basically for now, I'll split it up after I have a functioning game. Thanks.
#ifndef RESOURCES_H
#define RESOURCES_H
class Die{
public:
staticint roll();
};
class character{
public:
int hp();
bool state();
};
class attack{
public:
int number();
};
#endif
#include <iostream>
#include <cstdlib>
#include <ctime>
#include "characters.h"
int Die::roll()
{
return rand()%4+1;
}
int character::hp()
{
int i=500;
int currenthealth = i-damage;
return currenthealth;
// Why not just return i - damage
// What is i supposed to be? Give it a more meaningful name
}
int attack::number()
{
return Die::roll() * 25;
}
Thank you, it compiles well now. damage was global to fix an error I had earlier needing it to be seen by two functions.
i is initialhealth, which I've changed it to.
Now I'm going to start implementing it, wish me luck. I'll probably have to post again before I'm all done. I just started programming last week and the object oriented stuff is hard for me.