invalid use of non-static member function ‘int Die::roll()’

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.

characters.h (bad name, I know)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <cstdlib>
#include <ctime>
#include "characters.h"

int damage;

int Die::roll()
{
	int result = rand()%4+1;
	return result;
}

int character::hp()
{
	int i=500;
	int currenthealth = i-damage;
	return currenthealth;
}

int attack::number()
{
	switch(Die::roll)
	{
		case '1':
			damage=25;
			break;
		case '2':
			damage=50;
			break;
		case '3':
			damage=75;
			break;
		case '4':
			damage=100;
			break;
	}
	return damage;
}

main.cpp (mostly empty for now)
1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include "characters.h"
#include <cstdlib>
#include <ctime>
using namespace std;

int main()
{
	return 0;
}

terminal error compiling:
1
2
3
4
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)
Last edited on
 
switch(Die::roll)


try:

 
switch(Die::roll())


roll is a function so it needs to be called as a function ie with brackets
I tried, but then I can't call it without an object
1
2
error: cannot call member function ‘int Die::roll()’ without object
   23 |  switch(Die::roll())
Ah. As you are trying to access roll() without an object, roll needs to be be defined as static.

1
2
3
4
5
6
7
8
9
class Die{
public:
	static int roll();
};

static int Die::roll()
{
	return rand()%4+1;
}

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 ‘static int Die::roll()’ to have static linkage [-fpermissive]
    9 | static int Die::roll()
      |                      ^
In file included from characters.cpp:4:
characters.h:9:22: error: cannot declare member function ‘static int Die::roll()’ to have static linkage [-fpermissive]
    9 | static int Die::roll()
      |                      ^
characters.cpp:8:5: error: redefinition of ‘static int Die::roll()’
    8 | int Die::roll()
      |     ^~~
In file included from characters.cpp:4:
characters.h:9:12: note: ‘static int Die::roll()’ previously defined here
    9 | static int Die::roll()

characters.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#ifndef RESOURCES_H
#define RESOURCES_H

class Die{
public:
	static int roll();
};

static int Die::roll()
{
	return rand()%4+1;
}

class character{
public:
	int hp();
	bool state();
};

class attack{
public:
	int number();
};

#endif 
Last edited on
Sorry. My bad. You only use static in characters.h, not when you provide the class function body.

This compiles OK with VS:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <cstdlib>

class Die {
public:
	static int roll();
};

class character {
public:
	int hp();
	bool state();
};

class attack {
public:
	int number();
};

int damage;

int Die::roll()
{
	//int result = rand() % 4 + 1;
	return rand() % 4 + 1;
}

int character::hp()
{
	int i = 500;
	int currenthealth = i - damage;
	return currenthealth;
}

bool character::state() { return false; }

int attack::number()
{
	switch (Die::roll())
	{
		case '1':
			damage = 25;
			break;
		case '2':
			damage = 50;
			break;
		case '3':
			damage = 75;
			break;
		case '4':
			damage = 100;
			break;
	}
	return damage;
}

int main()
{
	return 0;
}

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;
}

Last edited on
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.
Oh, thanks, that'll save some time.
Have characters.h as

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#ifndef RESOURCES_H
#define RESOURCES_H

class Die{
public:
	static int roll();
};

class character{
public:
	int hp();
	bool state();
};

class attack{
public:
	int number();
};

#endif  


and characters.cpp as:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <cstdlib>
#include <ctime>
#include "characters.h"

int damage;

int Die::roll()
{
	return rand()%4+1;
}

int character::hp()
{
	int i=500;
	int currenthealth = i-damage;
	return currenthealth;
}

int attack::number()
{
    return damage = Die::roll() * 25;
}


Why have damage as a global variable in characters.cpp? You don't need it and using global variables isn't particularly good practice.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#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;
}

Last edited on
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.
Have fun!
Registered users can post here. Sign in or register to post.