Question

Hi everyboy :) I m pretty new on programming and especially on C++. This is why I am trying to test some beginners functions like getters and setters with private variable which are, in fact, pointers. I m doing this tests for an assignement and i have to use enumeration too.
I got 0 compilation error but my test program is crashing when i m trying to set my private pointer with an enum type.

Here is my class "Slot": header file : Slot.hpp

#ifndef SLOT_H
#define SLOT_H

enum Pawn{YELLOW, RED};

class Slot
{
public:
    Slot();
    ~Slot();
    Slot(const Slot& slot_to_copy) {color = new Pawn(*(slot_to_copy.color));}

    void setColor(Pawn);
    Pawn getColor();

private:
    Pawn *color; 
};

#endif

Here is my source file: Slot.cpp:

#include <iostream>
#include "Slot.hpp"

using namespace std;

Slot::Slot() {color = NULL;}

Slot::~Slot() {}

void Slot::setColor(Pawn col)
{
    *color = col;
}

Pawn Slot::getColor()
{
    return *color;
}

And here is my main where I am testing the getter and the setter:

#include <iostream>
#include "Slot.hpp"

using namespace std;

int main()
{
    Slot test;
    test.setColor(YELLOW); // testing the setter
    Pawn coin = test.getColor(); // testing the getter
    if (coin == YELLOW)
        cout <<"SUCCESS"<< endl;
    return 0;
}

My problem is located on the setter function ... Thank you in advance for your help, i already took 3 hours by trying to fix my problem and I cannot work on my assignement without this working correctly :/ Sorry for my approximate English by the way.

PS: I HAVE TO use pointer for that, because it is asked by my teacher and consist in the difficulty (or at least a part of it) of our programming assignement.

No correct solution

OTHER TIPS

Your default constructor Slot() doesn't allocate space for the Pawn *color, so it'll just point to some random location in memory. When you write to it you'll write to this random location.

You can set it like this:

Slot::Slot() : color(new Pawn)
{}

Here we're using the member initializer list to set color to allocated memory before the body of the constructor is executed. It's conceptually the same as saying:

Slot::Slot()
{
  color = new Pawn;
}

For integral types and pointers there's not much difference. However, if the member variables were class instances then the member variable initializer list is the only way to initialize classes that do not have a default constructor.

However, since enum is effectivly an int there's no point is using a pointer. Just declare it as:

Pawn color;

And get/set it like this:

void Slot::setColor(Pawn col)
{
    color = col;
}

Pawn Slot::getColor()
{
    return color;
}

You are not initializing the pointer in the default constructor. This means de-referencing it is undefined behaviour. You should make it point to something you can write to later:

Slot::Slot() : color(new Pawn()) { }

Note that having a class manage dynamically allocated resources means it has to do sensible things in its copy constructor, assignment operator, and destructor. See the rule of three.

The easiest solution in not to use a pointer at all:

....
private:
    Pawn color; 

Your color pointer is being assigned to something only in copy constructor, while in your main you are creating Slot using default constructor, that's why pointer stays uninitialized and behaviour of functions like setColor is undefined.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top