Derived class member function not being called

Pages: 12
So I posted a very similar question previously about this, but a fix that worked at the time has not stopped the same issue resurfacing in a different part of the code. Quick summary, in a text based RPG I am using an inputoutput class to handle user inputs with any derived class from a base area class, the 2 i am focusing on getting working at the moment are a village and a village outskirts.

In short the available_interactions_for_area function works for the village class, but when trying to do the same for the village outskirts class the function just won't run, and reverts to the village method. When the village method is commented out it still won't run it for the village_outskirts class, and reverts to a base method I've got printing a string to check. I can't see where I've gone wrong as my syntax for introducing the function is the same in both derived classes.

This is the inputoutput function for interacting with rooms, with the query highlighted.
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
int inputoutputhandler::interact_with_area(player& p) {
	
	area &area_right_now = p.get_current_area();
	cout << area_right_now.get_description() <<"\n"<<endl;

	//checking the area is kept track of by the player correctly
	//when the program is running with the area set to the village outskirts, this check shows that
	cout << "should be inside the "<< area_right_now.get_type() << endl;

	bool still_in_area(true);
	do {
		cout << area_right_now.get_menu_info() << endl;

		bool check = false;
		int answer;
		//get number of legal options for interacting with the room
		int b = area_right_now.get_number_of_room_options();

		//accounting for incorrect input
		while (!check)
		{
			check = true;

			cin >> answer;

			if (cin.fail()||answer>b)
			{
				cin.clear();
				cin.ignore();
				cout << "Please enter one of the given options" << endl;
				check = false;
			}

		}
			//exit function and move to area "waiting room" fn
			if (answer == 0) { still_in_area = false; }
			else {
				
				//QUERY IS HERE 
				//this function, when called for the village outskirts, runs the method for the village instead
				//with the function commented out for the village class, it runs the base class method
				area_right_now.available_interactions_for_area(answer, area_right_now, p);
				}
			

			

		
	} while (still_in_area);

	return 0;
}


And my village_outskirts.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
26
27
28
29
30
#pragma once
#include "area.h"
#include "player.h"
#include <vector>
class village_outskirts:public area
{
private:
	vector <NPC*> vector_of_NPCs;
	int grating_found{ 0 };
	int grating_unlocked{ 0 };
public:
	village_outskirts();
	~village_outskirts();
	string get_menu_info() const;
	string get_neighbouring_rooms() const;
	NPC & getNPC(int i, const area& a) const;
	string get_type()const;
	string get_description()const;

	//this is the function that won't run below
	void available_interactions_for_area(int i, area& a, character& p);

	int player_has_found_grating();
	int get_grating_discovery_flag()const;
	int get_grating_unlocked_flag()const;
	int player_has_key();
	int get_number_of_room_options();


};


Any help would be greatly appreciated as without getting this function to work any further developments would be severely hindered.
If control is being passed to village then that must mean the object is of type village. There's no way for control to be passed to an unrelated class unless you had memory corruption somewhere. Are you sure village_outskirts::get_type() is returning the correct value?
It's returning a string that is unique to the class, and does output village outskirts as being the correct type. The program in its current state is only using the village outskirts as the cuurent area as that's the one I'm trying to get working, so I don't know why it reverts to the base class function.
The program, at every check stage, outputs "village_outskirts" as the get_type() function, just as soon as it hits that function it outputs the string from the base member function and I'm pretty lost lmao
Can you upload the complete code so that we can run it.
With a debugger it's probably much easier to see.
Ok it's a bit of a long shot as there's a lot of header files and the like, so I'd have to post a link to a downloadable zip file.

Important information to know before running:

-Normally the game would start you in the village, but for purposes of testing the function I start in the village_outskirts (the derived class having issues with its available_interactions_for_area).

To start in the village, change the int argument on line 28 of game.cpp to 1 and uncomment the available_interactions_for_area function for the village, this would give an indication of how the game should run ideally from the start.

-Inputoutputhandler deals with generic player - room interactions, and game.cpp sets up all the initial conditions of a new game needed for the inputoutputhandler to take over.

-My program needs to compile on windows visual studio for submission, so I've used _getch() in there, (any non windows users would need to comment these out, I think htey're just in the game.cpp file), will post a link in a minute if that's cool w people
Last edited on
but when trying to do the same for the village outskirts class the function just won't run, and reverts to the village method.


This would never happen if you are passing a village_outskirts object. Village_outskirts and village are not related classes. They are siblings in the inheritance hierarchy with a common parent. Thus, village_outskirts knows nothing about village and vice versa.

The fact that it is calling the village's method and then, when that method is deleted, calling the base method on a village_outskirts object tells me that you are not passing a village_outskirts object at all, but you are most likely passing a village object. Double check your code and try to run some test code where you explicitly add a village_outskirts object to the player area vector and try it.
Last edited on
Hey toaster, what you have suggested is exactly what I've tried, and the output checks for the type when I have explicitly added the outskirts object to the player result in the correct output. It is only on that function that things go wrong. The link provided has it set up so that the player vector is initialised with an object of type village_outskirts. If I uncomment the village_outskirts function that is when it replaces the base class method, even if I have not initialised the player area vector to the village at all.
If it was passing a village object thoughout the function the type checks would say it is a village, but all the way up until that function it claims to still be a village_outskirts object, hence my bewilderment
What is your player::set_current_area method even doing? It is taking an area object by reference and setting its internal pointer to that area. The vector_of_areas is in the inputoutput class and player has no knowledge of it.
I use it for the start of the game, since the vector_of_areas is a vector of pointers to area objects they are all defined in the inputoutput class, which I guess is the equivalent of a MAP class that holds and switches out all the rooms the player can be in. I use the set_current_area to initialise the player's location in the game.cpp file for the start of the game, where I define a new player and inputoutputhandler object.
I coudn't even get your code to compile because you forgot to give me map.h.

Also, try writing some code that manually pushes a village_outskirts object into the vector and then calls the method on it from the vector itself in that same function, for example, do this in the inputoutput constructor.
Last edited on
You shouldn't even need map.h, I created and removed that at the start of the project, it's not in my list of files in visual studio
Some of your files still have dependencies that try to include "map.h"

Also, try writing some code that manually pushes a village_outskirts object into the vector and then calls the method on it from the vector itself in that same function, for example, do this in the inputoutput constructor. Once you have established that village_outskirts indeed calls its method, then you can look at your code for player and make sure it is correctly.

Also, your player class seems to set a bunch of data members (health, flags, etc), but those data member are not in the class.

I don't think anybody here will be able to navigate through the cruft of your code and find the problem, especially when we can't get it to compile. Make a few test cases with actual village_outskirts objects (not through the vector), and post the results of those test cases here.
Last edited on
aaah doing what you suggested does actually call the function...
Last edited on
See? Then the problem is somewhere with your code. It is most likely with the code that sets the current area in the player class. AKA, either the set_current_area method itself or the code that calls set_current_area
Last edited on
I don't see any links.
Link added just now
1 question I have about the area fns, when using the move_area function in the player class, or even set_current_area, do I need to clear the current object being pointed to from the player's memory somehow, i.e with a delete function? Not sure if simply redefining the pointer in the player class is enough, but had a lot of compilation errors trying to add deletes in, still quite new to cpp and programming in general (this is my first major project) so apologies for any potentially dumb qns.
Pages: 12