Can you call a function from inside a class function?

Good morning everyone.
Have a question about calling one of my dice roll functions from inside a class i have made.
Is this possible to do with C++?

I'm getting identifier not found errors. for d10Roll()

Is there a different way i need to go about this?

1
2
3
4
5
6
7
8
9
10
void TheDarklake::getCollision() {

	int d10a = d10Roll();
	int d10b = d10Roll();
	int d10c = d10Roll();

	std::cout << "------------------------------ Collision -----------------------------" << std::endl;
	std::cout << "This terrain encounter occurs only if one or more party members are " << std::endl;
	std::cout << "traveling by raft or boat, and there's a strong current. Have everyone" << std::endl;
}  
Last edited on
if d10Roll is within the class, you may have forgotten to specify the function in the header

1
2
3
4
5
6
class TheDarklake
{
void getCollision();
void d10Roll();

}


Or you might have forgotten the implementation of that function
1
2
3
4
5
6
7
8
9
10
TheDarklake::d10Roll()
{
   //Roll Code
}
TheDarklake::getCollision()
{
// collision code


}

also make sure you have specified the d10Roll above the getCollision

Hope that helps
Ok i understand now.
So, I'd have to create a d10Roll(); function in every class I create that uses a dice roll?
I can't use the random dice functions I already have in my main source.cpp file?

You can write the implementation of d10Roll in TheDarkLake or whichever class you prefer and then include the header of that class into another class

so if you have source.cpp

1
2
3
4
5
6
7
8
9
10
11
12

#include "TheDarklake.h"


int main()
{
 TheDarklake darklake;
darklake.d10Roll();

}




if you dont like that approach you can do something called forward declaration.
Given that the class which called TheDarklake class will have the d10Roll() implementation you can do something like this

source.cpp

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


void d10Roll()
{

	std::cout << "calling roll" << std::endl;
}


int main()
{
	TheDarkLake dk;


	system("pause");
	

}


TheDarkLake.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include "TheDarkLake.h"

void d10Roll(); // forward declaration here promising the compiler that this function indeed exists later in the code.

TheDarkLake::TheDarkLake()
{
	d10Roll();

}


TheDarkLake::~TheDarkLake()
{
}




Still don't think i'm going about it corectly.
What do you think about this code example?

source.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
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <cstdlib>
#include <ctime>

int d6Roll();
int d10Roll();

int main() {

	srand((unsigned)time(NULL));

        TheDarklake darklake;
	darklake.d10Roll();

        darklakeTerrainEncounters();

        system("PAUSE");
	return 0;
}

int d6Roll() {
	int d6_result;

	d6_result = rand() % 6 + 1;

	return d6_result;
}

int d10Roll() {
	int d10_result;

	d10_result = rand() % 10 + 1;

	return d10_result;
}


void darklakeTerrainEncounters() {

	int d6 = d6Roll();

	std::cout << "--------------------- Darklake Terrain Encounter ---------------------" << std::endl;
	std::cout << "Darklake Terrain Encounter Roll = " << d10 << std::endl;

	if (d6 == 1) {
		//Collision
		darklake.getCollision();
		std::cout << std::endl;
	}
}

        


TheDarklake.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
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>

#ifndef TheDarklake_H
#define TheDarklake_H



class TheDarklake {
public:

	TheDarklake();
	~TheDarklake();

	int d10Roll();

	// The Darklake Terrain Encounters
	void getCollision();

private:

}


TheDarklake.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include "TheDarklake.h"


TheDarklake::TheDarklake() {

	d10Roll();
}

TheDarklake::~TheDarklake() {}


// The Darklake Terrain Encounters
void TheDarklake::getCollision() {

	int d10a = d10Roll();
	int d10b = d10Roll();
	int d10c = d10Roll();

	std::cout << "------------------------------ Collision -----------------------------" << std::endl;
	std::cout << "Collision Damage is "<< d10a + d10b + d10c << std::endl;
        std::cout << std::endl;
}


Last edited on
Topic archived. No new replies allowed.