Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9190493
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:28:29+00:00 2026-06-17T20:28:29+00:00

I’ve looked up all sorts of guides and tutorials on classes and constructors, but

  • 0

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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T20:28:30+00:00Added an answer on June 17, 2026 at 8:28 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
I have a text area in my form which accepts all possible characters from
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I want to construct a data frame in an Rcpp function, but when I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.