1

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.

#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 function to 
// call CHARBOX. Which I don`t understand completely.
        // Constructor.
    CHARBOX(int posx, int posy, int width, int height, int colour)
    {
        DrawBox(posx, posy, width, height, colour);
    }

    // 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;
}
Joseph Quinsey
  • 9,553
  • 10
  • 54
  • 77

1 Answers1

1

You need to use a member initialization list to initialize your class members:

CHARBOX(int posx, int posy, int width, int height, int colour):posx_(posx),posy_(posy),width_(width),height_(height), colour_(colour)
{

}

Good Read:
What is this weird colon-member (" : ") syntax in the constructor?

someone advised me to name my class variables with a special symbol attached.

That is so that you can distinguish between the member variable name and the passed function argument name. It is not a necessity you can simply choose different names and it should be just fine.

why does it make sense to the program?

Constructor in C++ is a special member function which gets called whenever an object of the class is created. The purpose of the constructor is to provide an opportunity to properly initialize the members of the object. for e.x: width_, height_ etc in your case.

Once the object is constructed, the class members are assumed to be in valid and determinate state so that they can be used by the program. In your case unless you initialize the members in the constructor they will have indeterminate values i.e: any random values. You don't really want a member function to get the width_ and it to return a garbage value. So you need to initialize them.

Community
  • 1
  • 1
Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • Yeah, I was fiddling around the member initialization and trying to read about it, but could not get it to work, much like in this case, when I use the code you provided it still doesn't compile and returns errors along the lines: – Juris Zebnickis Jan 26 '13 at 05:55
  • main.cpp:127: no matching function for call DrawBox and – Juris Zebnickis Jan 26 '13 at 05:56
  • main.cpp:22: no candidates are: void CHARBOX... – Juris Zebnickis Jan 26 '13 at 05:57
  • @JurisZebnickis: Think about it. You are calling the function `DrawBox` in the constructor but the function `DrawBox` is declared & defined *after* the constructor body. How will the compiler know what `DrawBox` is that you call inside the constructor? It cannot because at that point of time it does not know `DrawBox`. Move the definition of `DrawBox` before the constructor and then compiler knows it. – Alok Save Jan 26 '13 at 05:58
  • @JurisZebnickis: There is a lot that needs to be changed here. Please consider starting with a [good book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list).You cannot learn the language on a programming forum, You must read a good book for it. – Alok Save Jan 26 '13 at 06:02
  • @JurisZebnickis: `player.DrawBox();` calls a function which takes no parameters, while you define a function `DrawBox()` to take 5 input parameters, How do you think the compiler should call it if you don't pass any? – Alok Save Jan 26 '13 at 06:07
  • thanks, that is what I`m trying to do. Sometimes I feel like I need some kind of a more practical example and then I go watch a tutorial. I found these quite useful for example: http://thenewboston.org/list.php?cat=16 , but the constructor tutorial there doesn`t cover the same scope of what I`m trying to do and I feel like I`m missing something. – Juris Zebnickis Jan 26 '13 at 06:11
  • Erm. I believe that I do in fact pass in parameters. As I explained in the original post. The parameters are given when I initialize my class products later in the program and I need to do it like that because I need two start locations for both boxes and then I need to continuously redraw one of them for animation. So I can`t just fill in the variables with values where I initialize the variables. – Juris Zebnickis Jan 26 '13 at 06:14