How can I call a function in another class?

I need to call a function that is in another class but I dont know how.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void Monster::killMonster(){}
void Level::movePlayer(int x ,int y,bool newPos)
{
       if(newPos)
	{
		if(MyLevel[x][y]==2)
		{
			MyLevel[x][y] = 0;
			cout << "You killed monster" << endl;
			killMonster(); // I need to call the killMonster function here which is in the monster class.
		}
		MyLevel[x][y] = 1;
	}
	else
		MyLevel[x][y] = 0;
}


The function is in a different class and I dont know how to call it =( I have been trying to figure this out since last week but I dont know what to do or where to look.
Last edited on
If the function is a non-static member function you will have to have an instance of Monster to call the killMonster function.
i'm really new to c++ programming, but as far i know in order to use other class functions in another class you have to make a base and derived class, then you have acess to everything in public and protected.

class Monster{ <<< base class
};

class Level: public Monster{ <<<derived class
};

monster is base class and Level is derived class, Level can use everything that monster class has in public and protected.

hope it helps
Peter87 Dude, the function is a non static member. How do I create an instance of this class?

IronmanCro thanks for the help but thats not what I need.
two ways:

make the monster function friend.

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
#include<iostream>
using namespace std;

class dunno{
      
public:
       
      friend void monster() { cout << "LEROOOOOOOOOOOOOOOY JEEEEEEEENKIIIIIIIINS!" << endl; }
       
       
};


class Dolan {
      
      public:
             
        void dolan() { monster(); cout << "leroi pls" << endl; }
        
};

int main() {
    
Dolan bog;

bog.dolan();
    
    
cin.get();
return 0;   
}






second way: use inheritance

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

#include<iostream>
using namespace std;

class dunno{
      
public:
       
       void monster() { cout << "LEROOOOOOOOOOOOOOOY JEEEEEEEENKIIIIIIIINS!" << endl; }
       
       
};


class Dolan:public dunno {
      
      public:
             
        void dolan() { monster(); cout << "leroi pls" << endl; }
        
};

int main() {
    
Dolan bog;

bog.dolan();
    
    
cin.get();
return 0;   
}







you cannot use the monster function any other way I think.
Last edited on
Inheritance is not appropriate here. A Level is not a Monster.

You can only call Monster's non-static member functions on instances (also called objects) of Monster. There are many ways to create instances but in the simplest way it would look like
Monster monster;
Now you can call the killMonster() member function on this instance.
monster.killMonster();

In your code you probably don't want create a Monster instance just to call killMonster() on it right away. Well, I don't know what killMonster() does but my guess is that you have some Monster instances stored somewhere and you want to kill one of them by calling killMonster() on it, so what monster is it that you want to kill?
Last edited on
In your Level::movePlayer(...) function, I would add a monster parameter, if you have access to the monster object from before you called this function. Like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void Monster::killMonster(){}
void Level::movePlayer(int x ,int y,bool newPos, monster myMonster)
{
       if(newPos)
	{
		if(MyLevel[x][y]==2)
		{
			MyLevel[x][y] = 0;
			cout << "You killed monster" << endl;
			myMonster.killMonster(); // I need to call the killMonster function here which is in the monster class.
		}
		MyLevel[x][y] = 1;
	}
	else
		MyLevel[x][y] = 0;
}


I'm not sure if that will work because I don't know what the rest of your code looks like.

Or, when you create an instance of you class Monster, you can add a search function to find the monster in that cell (maybe?)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void Monster::killMonster(){}
void Level::movePlayer(int x ,int y,bool newPos)
{
       if(newPos)
	{
		if(MyLevel[x][y]==2)
		{
			MyLevel[x][y] = 0;
			cout << "You killed monster" << endl;
                        Monster tempMonster = searchForTheMonster(...);
			tempMonster.killMonster(); // I need to call the killMonster function here which is in the monster class.
		}
		MyLevel[x][y] = 1;
	}
	else
		MyLevel[x][y] = 0;
}


Again, I don't know if this will work because I have no idea how the rest of your program is structured.
Last edited on
Thanks for the help guys but still having no luck. I have a player class, a monster class and a level class and a loop function. The loop function gets the position of the player and the monster and gives it to the level which has a 2d array and puts the player and monster in there. The loop keeps on getting the position of the monster and puts in in the level, I need it to stop doing this when the monster is dead. I cannot figure out how to do this.

Here is the project solution.

https://rapidshare.com/files/3039430843/Text_Game.rar
anyone?
It depends on whether there's more than one monster. Now, I haven't looked at the project solution, but if there is supposed to be more than one, just have an array of monsters, and if a monster dies, remove it from the array.

If there's only one, then have it call its destructor when it dies.

Within the loop, if the monster dies, then killMonster.

So something like:

begin loop
check to see if monster exists (a simple ==NULL would work here I think)
if it does, take position of it
do whatever else happens
if monster is supposed to die, call the function (within the killMonster() function, have it call the destructor, which would perform a this=NULL statement)
do other stuff
end loop?
I dont know how to call a function that is in the monster class from the level class, this is my main problem.

I have a loop which gets the player position and the monster position and puts it in the level array. I need to set a variable in the monster class to false so the loop doesnt keep on getting the monster position and keep on adding it to the level if the monster is dead.

If you are only going to have one Monster in each Level you can add a Monster objects as a member variable of Level.

1
2
3
4
5
6
7
class Level
{
	...
private:
	Monster monster;
	...
};

Then you can call the killMonster function on monster by doing monster.killMonster();.
Last edited on
nope. i need to add 10 monsters
Do you know how to create an array?
okay so I figured out how to get rid of 1 monster and now Im trying to add 10 monsters and I can set the array to 0 where the monster was but it comes back after 2 loops =(
I return alive variable when the player hits the monster

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
bool Level::movePlayer(int x ,int y,bool newPos)
{
bool alive = true;
if(newPos)
{
	if(MyLevel[x][y]==2)
	{	
		cout << "You killed a monster" << endl;
		alive = false;
		cout << endl;
	}
	MyLevel[x][y] = 1;
}
else
	MyLevel[x][y] = 0;
return alive;
}


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
void gameLoop(Player p,Level l,Monster mon[])
{
	bool loop = true;
	char choice;
	bool alive;

	while (loop)
	{
		cout << "Enter Direction: ";
		cin >> choice;
		if(choice=='q') 
		{
			loop=false;
		}
		for (i = 0; i < 10; i++)
		{
			if(mon[i].isAlive()) 
			{	
				l.moveMonster(mon[i].getMPosX(),mon[i].getMPosY(),false); // Set the array in which the monster is to 0.
				mon[i].moveMonster();
				l.moveMonster(mon[i].getMPosX(),mon[i].getMPosY(),true); // Set the array in which the monster is to 2.
			}
		}
		
		l.movePlayer(p.getX(),p.getY(),false); // Set the array in which the player is to 0.
		p.movePlayer(choice);
		alive=l.movePlayer(p.getX(),p.getY(),true); // Move the player and set the new array to 1.
		if(!alive) {
			mon[i].killMonster(); //Which monsters to I kill?
		}
		l.print();
	}
}


I dont know which monster is in which array and how to call the killMonster function of that monster object.

Last edited on
Topic archived. No new replies allowed.