How to update variable values of a member function from a constructor that initializes them?

I`ve looked up all sorts of guides and tutorials on classes and constructors, but so far it hasn`t made sense to me how to implement the combination of both into my program. I feel like some massive logic block is evading me. I would be incredibly thankful if anyone out there could explain in human language how should the constructor fill in the variables for my function. As in not just how to make it do what I want it to do, but why does it make sense to the program? I started studying this year. Thank you. This is a code to be compiled on a GBA emulator although the problem I have is purely from the perspective of C++. I have outlined the additional comments for this post in the program in bold. How I understand what I`m trying to do so far is:
I create a constructor which then gets the variable values for the function within in it from the main loop later in the program where I initialize the class objects for the first time and then in the part where I redraw the box for the movement I simply call upon the class objects which should have their initial values stored from the constructor.
**********
**********
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <stdint.h>
#include <stdlib.h>
#include "gba.h"

// A class with variables.
class CHARBOX
{
	public:
	int init_;
	int str_;
	int health_;
	int posx_;
	int posy_;
	int width_;
	int height_;
	int colour_; // someone advised me to name my class variables with a special symbol attached.

	public:
// This is probably the part where I have not done things right. When this is compiling, there is an error saying that there is no matching call to CHARBOX. Which I don`t understand completely.
        // Constructor.
	CHARBOX(int posx, int posy, int width, int height, int colour)
	{
		DrawBox();
	}
	
	// Drawing functions.
	void DrawBox(int posx_, int posy_, int width_, int height_, int colour_)
	{
		for (int x = posx_; x < posx_ + width_; x++)
		{
			for (int y = posy_; y < posy_ + height_; y++)
			{
				PlotPixel8(x, y, colour_);
			}
		}
	}
};

// The entry point.
int main()
{
	// Put the display into bitmap mode 4, and enable background 2.
	REG_DISPCNT = MODE4 | BG2_ENABLE;
	
	// Defining some colour palettes.
	SetPaletteBG(1, RGB(90,0,0));
	SetPaletteBG(2, RGB(0,90,0));
	SetPaletteBG(3, RGB(0,0,90));
	SetPaletteBG(4, RGB(90,90,0));
	SetPaletteBG(5, RGB(90,0,90));
	
//Here is where the objects get initialized and the constructor is called.
        // Draw the player at a starting location.
	CHARBOX player(10, 24, 6, 8, 1);
		
	// Draw the enemy at a starting location.
	CHARBOX enemy(80, 24, 6, 8, 2);
	
// main loop.
	while (true);
	{
		// Clear screen and paint background.
		ClearScreen8(1);
		
		// Flip buffers to smoothen the drawing.
		void FlipBuffers();
		
		// Redraw the player.
		if ((REG_KEYINPUT & KEY_LEFT) == 0)
		{
			player.DrawBox(); // This is where the object gets called again to be redrawn.
			posx_--;
		}
		
		if ((REG_KEYINPUT & KEY_RIGHT) == 0)
		{
			player.DrawBox();
			posx_++;
		}
		
		if ((REG_KEYINPUT & KEY_UP) == 0)
		{
			player.DrawBox();
			posy_--;
		}
		
		if ((REG_KEYINPUT & KEY_DOWN) == 0)
		{
			player.DrawBox();
			posy_++;
		}
	WaitVSync();
	}
return 0;
}

**********
**********
edit: sorry, didn`t see the source code formatting option.
Last edited on
You sure it isn't failing to find a matching calling for DrawBox()? As I see it, on line 23, 71, etc. you try to call that method with no parameters but I only see a definition taking 5.

It might be more helpful if you copy and paste the entire error message.
The parameters used for drawing should be stored from the line 54 where I initialize my class objects. No? Because I need the two charboxes to be drawn for the first time only using the parameters once, otherwise if I try redrawing them later using the same parameters it`s not gonna redraw anythin. As it was explained to me by someone, what I want to do is use the constructor to initialize and store the starting values. That`s where the logic loses me.
The full error message types that I get are:
main.cpp:71: error: no matching function for call to `CHARBOX::DrawBox()`
main.cpp:26: error: candidates are: void CHARBOX::DrawBox(int, int, int, int, int)
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
class CHARBOX
{
public:             // public data members?
	int init_;
	int str_;
	int health_;
	int posx_;
	int posy_;
	int width_;
	int height_;
	int colour_; 

	public:
	CHARBOX(int posx, int posy, int width, int height, int colour)
        : init_(), str_(), health_(), posx_(posx), posy_(posy), 
          width_(width), height_(height), colour_(colour)
	{
		// DrawBox();
	}
	
	void DrawBox() const
	{
		for (int x = posx_; x < posx_ + width_; x++)
		{
			for (int y = posy_; y < posy_ + height_; y++)
			{
				PlotPixel8(x, y, colour_);
			}
		}
	}
};


If a method should take no parameters, don't indicate that it should. Pay attention to compiler warnings. You should supply values for init_, str_ and health in your constructor.
Ok I got to a point where the code compiles and draws my two objects at their starting positions, but now I can`t figure out how to make the method I am calling when I redraw the box in a new position have the values updated. Essentially what I think is happening is that every time I am calling for the method DrawBox it just takes its original values and keeps drawing the boxes at the same spot. I WANT TO UNDERSTAND THIS. T_T

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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <stdint.h>
#include <stdlib.h>
#include "gba.h"

// A class with variables for the characters.
class CHARBOX
{
	public:
	int init_;
	int str_;
	int health_;
	int posx_;
	int posy_;
	int width_;
	int height_;
	int colour_;

	public:
	// Constructor.
	CHARBOX(int posx, int posy, int width, int height, int colour)
        : init_(0), str_(0), health_(0), posx_(posx), posy_(posy), 
          width_(width), height_(height), colour_(colour)
	{
		DrawBox();
	}
	
	// Drawing functions.
	void DrawBox()
	{
		for (int x = posx_; x < posx_ + width_; x++)
		{
			for (int y = posy_; y < posy_ + height_; y++)
			{
				PlotPixel8(x, y, colour_);
			}
		}
	}
};

// The entry point for the game.
int main()
{
	// Put the display into bitmap mode 4, and enable background 2.
	REG_DISPCNT = MODE4 | BG2_ENABLE;
	
	// Defining some colour palettes.
	SetPaletteBG(1, RGB(90,0,0));
	SetPaletteBG(2, RGB(0,90,0));
	SetPaletteBG(3, RGB(0,0,90));
	SetPaletteBG(4, RGB(90,90,0));
	SetPaletteBG(5, RGB(90,0,90));
	
	// Draw the player dude-box at a starting location.
	CHARBOX player(10, 24, 6, 8, 1);
		
	// Draw the enemy dude-box at a starting location.
	CHARBOX enemy(80, 24, 6, 8, 2);
	
	while (true);
	{
		// Flip buffers to smoothen the drawing.
		void FlipBuffers(); // This is just a default GBA function.
		
		// Clear screen and paint background.
		ClearScreen8(1); // And so is this.
	
		// Redraw the player dude-box.
		if ((REG_KEYINPUT & KEY_LEFT) == 0)
		{
			player.posx_--;
			player.DrawBox();
		}
		
		if ((REG_KEYINPUT & KEY_RIGHT) == 0)
		{
			player.posx_++;
			player.DrawBox();
		}
		
		if ((REG_KEYINPUT & KEY_UP) == 0)
		{
			player.posy_--;
			player.DrawBox();
		}
		
		if ((REG_KEYINPUT & KEY_DOWN) == 0)
		{
			player.posy_++;
			player.DrawBox();
		}
	WaitVSync();
	}
return 0;
}
Last edited on
Is FlipBuffers() important to get the result you want? On line 62 you are not actually calling the function; remove the return value to actually call it.
It doesn`t change anything. I tried just commenting out both FlipBuffer and ClearScreen functions, but the result was still the same. As in the code compiles alright and draws my two objects (player and enemy) as boxes at their starting positions, but they wouldnt redraw in any new positions when I press the input buttons. Which leads me to believe that the problem is not within the technicalities of the drawing, but within the logic of my functions.

Also I don`t really understand what you mean. FlipBuffers is a default GBA function that`s in its header file and it doesn`t return anything. It simply flips between the two REG_VIDEO_PAGE entries available to the GBA in Mode4.
Last edited on
What are you doing to get input there? It looks like you're doing a bit-wise and of two constants and somehow expecting those values to change each iteration of the loop.
Last edited on
It`s said before that it is a code that compiles on a GameBoyAdvance emulator. All the parts that look like they are wrong in C++ are actually right. The two `constants` here are just pointers to the GBA button memory. Input basically says if a key is pressed { do this }.
while (true);

Maybe get rid of that semi-colon.
Derp.




That solves original problem. Thank you for noticing that. My next problem however... brings me over to the GBADEV forums I think.
Topic archived. No new replies allowed.