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

  • SEARCH
  • Home
  • 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 7847961
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T18:03:20+00:00 2026-06-02T18:03:20+00:00

I’m designing a simple Connect 4 game. So far, I have 4 underlying classes:

  • 0

I’m designing a simple Connect 4 game. So far, I have 4 underlying classes:

Colour – responsible for representing colours (RGBA). Includes conversion operators.

Player – represents a player of the game. Each Player has a Colour and a name.

Board – represents the playing board. It contains dimensions, as well as a 2D vector of Tiles with those dimensions.

Tile – a nested class within Board. Represents one space on the board. Each Tile has a Colour and an std::unique_ptr to the owner of that tile. The owner starts as nullptr and can be changed once to a Player. The colour starts as a transparent black.


I’ve tested my Colour class and it appears to be working fine. My Player class is in tip-top shape as well. However, I’m having some problems with the Board/Tile classes.

My test consisted of creating two players, and a board. These executed normally. Next, I loop through the dimensions of the board, once for each tile. I then call

board.tile (j, i).claimBy (p2); 

The loop goes through rows with i and columns with j, the way you’d expect to print it.

tile (j, i) retrieves the tile I’m working with. It works as expected.


Chain of Events Leading to the Crash:

claimBy (p2) sets the tile to become claimed by player 2. It is implemented as follows:

bool Board::Tile::claimBy (const Player &owner)
{
    if (!_owner)
    {
        *_owner = owner;
        _colour = owner.colour();
        return true;
    }

    return false;
}

_owner is my std::unique_ptr<Player>. It first checks whether the owner of the tile has been set before (i.e. is not nullptr). If not, it sets the Player inside to the one passed in. It then updates the tile’s colour and returns true. If the tile has been previously claimed, it returns false.

Following the debugger, the crash occurs in the line *_owner = owner;. Stepping in takes me to the line struct Player (my declaration of the Player class), which I take to be the implicit copy constructor (remember the class only has a Colour _colour and a std::string _name).

Stepping in again leads me to Colour::operator= (which makes sense for a copy constructor to call). Here’s the definition:

Colour &Colour::operator= (const Colour &rhs)
{
    if (*this != rhs)
    {
        _red = rhs.red();
        _green = rhs.green();
        _blue = rhs.blue();
        _alpha = rhs.alpha();
    }

    return *this;
}

The path turns into *this != rhs. This is just a reverse call to operator==, which is:

return red() == rhs.red()
    && green() == rhs.green()
    && blue() == rhs.blue()
    && alpha() == rhs.alpha();

The first comparison here red() == rhs.red() has red() which is just return _red;. This is the point at which the program crashes. The debugger states that this (this->_red) is 0x0.

I’m clueless about why this is happening. My best guess is that I’m using the smart pointer wrongly. I’ve never actually used one before, but it should be pretty similar to normal pointers, and I didn’t think release would accomplish anything if the pointer is nullptr.

What could be the cause of this being 0x0?

Edit:
I’m sure everything is initialized, as I do so in each constructor, in member initializers (e.g. Board::Tile::Tile() : _colour (Colours::NONE), _owner (nullptr){}), where NONE is a transparent black.

I’m also not too proficient with a debugger, as I haven’t used it that much over printing debugging values.

  • 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-02T18:03:20+00:00Added an answer on June 2, 2026 at 6:03 pm

    The line

    *_owner = owner;
    

    means “make a copy of the owner object, and store it at the place that _owner points to.” The problem is that _owner doesn’t point to anything yet; it’s still null.

    If you really want to make a copy of the Player object in each tile that the player controls, you’d need to do

    _owner.reset(new Player(owner));
    

    But making copies of the Player object is a strange thing to do. Consider using shared_ptr instead — you can have both owner and _owner be shared_ptrs, and just assign one to the other in the usual way.

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

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
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 have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

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.