0

I have an enumeration data type as follows: enum class CellColor {white, black};.

I need to write a public member function that shall be named get_color and shall return type CellColor and shall accept no input parameters. This function will also return the value of Cell's color member.

I've tried writing it out dozens of times, but I am still encountering errors.

Here is what I have so far:

#ifndef HAVEYOUSEENTHSISNAIL_CELL
#define HAVEYOUSEENTHSISNAIL_CELL

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


class Cell {        // This class is used to define the NxN board in the final solution.
public:
    Cell() {        // DEFAULT CONSTRUCTOR
        enum CellColor {white, black};
        CellColor color = white;        // Shall set color to white upon construction
    }

    void change_color() {       // PUBLIC MEMBER FUNCTION FOR COLOR CHANGE
        enum CellColor {white, black};
        CellColor color;

        if (color == white) {
            color = black;
        }
        else if (color == black) {
            color = white;
        }
        return;     // Return type void & accepts no parameters
    }

    CellColor get_color() {     // PUBLIC MEMBER FUNCTION FOR CURRENT COLOR
        enum CellColor {white, black};
        CellColor color;
        return color;
        // Shall return type CellColor
        // Shall return the value of the Cell's Color member
    }

    std::string get_color_string() {        // PUBLIC MEMBER FUNCTION FOR CURRENT COLOR STRING
        enum CellColor {white, black};
        CellColor color;
        if (color == black) {
            std::string black = "1";
            return black;       // Returns type std::string
        }
        else if (color == white) {
            std::string white = "0";
            return white;       // Returns type std::string
        }
    }
private:
    enum class CellColor {white, black};        // Scoped enum: enum [class] [structure] {enum list};
    CellColor color;        // Creating CellColor type variable color
};

#endif

Any suggestions on how to write an enum type function that also returns an enum type?

3 Answers3

3

When you define the enumeration and the variable inside the function, you define brand new and distinct entities that are separate from the member enumeration and variable.

If we take your constructor:

Cell() {        // DEFAULT CONSTRUCTOR
    enum CellColor {white, black};
    CellColor color = white;        // Shall set color to white upon construction
}

CellColor is a unique enumeration whose scope is inside the Cell Constructor only. And the same with the color variable, whose life-time ends when the function returns.

Instead you can just assign to the member variable:

Cell() {        // DEFAULT CONSTRUCTOR
    color = white;        // Shall set color to white upon construction
}

Or better yet (at least for the constructor) use a member initializer list:

Cell() : color{ white } {
    // Empty
}

None of your other member functions should define the enumeration or the variable.

Like for example:

void change_color() {       // PUBLIC MEMBER FUNCTION FOR COLOR CHANGE
    if (color == white) {
        color = black;
    }
    else if (color == black) {
        color = white;
    }
}
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
1

Do not define the enum everytime you want to use it. Also when you want to access the member color you always declare a local variable of same name and use that instead. Don't do that. Moreover you need to specify the scoped enums type when spelling out the name of one of its members, eg CellColor::white. If you don't want that you could use an old unscoped enum.

#include <string>

class Cell {
public:
    enum class CellColor {white, black};

    Cell() : color(CellColor::white) {}

    void change_color() {
        if (color == CellColor::white) {
            color = CellColor::black;
        }
        else if (color == CellColor::black) {
            color = CellColor::white;
        }
    }

    CellColor get_color() {
        return color;
    }

    std::string get_color_string() {
        if (color == CellColor::black) {
            return "1";
        }
        else if (color == CellColor::white) {
            return "0";
        }
    }
private:
    CellColor color;
};
463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
0

Only declare enum CellColor {white, black}; once within the class.

The error was not declared in this scope comes from declaring the enum as an enum class. Using CellColor::white solves this problem.

See https://stackoverflow.com/a/39317849

Remove CellColor color; everywhere except under private.

When you declare CellColor color in the function, you are using the local variable (limited to that method) rather than the member variable (Cell::color).

#ifndef HAVEYOUSEENTHSISNAIL_CELL
#define HAVEYOUSEENTHSISNAIL_CELL

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


class Cell
{               // This class is used to define the NxN board in the final solution.
public:

  enum class CellColor
  {
    white, black
  };                // Scoped enum: enum [class] [structure] {enum list};

    Cell ()
  {             // DEFAULT CONSTRUCTOR
    color = CellColor::white;       // Shall set color to white upon construction
  }

  void change_color ()
  {             // PUBLIC MEMBER FUNCTION FOR COLOR CHANGE

    if (color == CellColor::white)
      {
    color = CellColor::black;
      }
    else if (color == CellColor::black)
      {
    color = CellColor::white;
      }
    return;         // Return type void & accepts no parameters
  }

  CellColor get_color ()
  {             // PUBLIC MEMBER FUNCTION FOR CURRENT COLOR
    return color;
    // Shall return type CellColor
    // Shall return the value of the Cell's Color member
  }

  std::string get_color_string ()
  {             // PUBLIC MEMBER FUNCTION FOR CURRENT COLOR STRING
    if (color == CellColor::black)
      {
    std::string black = "1";
    return black;       // Returns type std::string
      }
    else if (color == CellColor::white)
      {
    std::string white = "0";
    return white;       // Returns type std::string
      }
  }
private:
  CellColor color;      // Creating CellColor type variable color
};

#endif
Chris Happy
  • 7,088
  • 2
  • 22
  • 49