0

i'm recently starting with c++ and I'm having troubles when I want to use a c++ class like Java's enums. I would have the 'simulated enum' class attribute, but when I try to initialize the attribute in the constructor i received the following error:

no default constructor exists for model::suite

I now I have the constructor private, but the enum should have private constructors to prevent the construction of undefined objects of that class.

¿What should I do?

e.g.

suite.h

#include <iostream>
#include <string>
#include <vector>

namespace model
{
    class Suite
    {
        public:
            static const Suite CLUBS;
            static const Suite DIAMONDS;
            static const Suite SPADES;
            static const Suite HEARTS;
            static const int SIZE = 4;

        private:
            std::string name;

            Suite(std::string name)
            {
                this->name = name;
            }

        public:
            std::string toString()
            {
                return name;
            }

            std::vector<Suite> values()
            {
                return {Suite::CLUBS, Suite::DIAMONDS, Suite::SPADES, Suite::HEARTS};
            }
    };
    const Suite Suite::CLUBS = Suite("CLUBS");
    const Suite Suite::DIAMONDS = Suite("DIAMONDS");
    const Suite Suite::SPADES = Suite("SPADES");
    const Suite Suite::HEARTS = Suite("HEARTS");
}

card.h

#pragma once
#include <string>
#include "suite.h"
#include "face.h"

namespace model
{
    class Card
    {
        public:
            Card(int face, int suite);
            Face getFace();
            Suite getSuite();
            bool isMergeable();
            std::string toString();

        private:
            Face face;
            Suite suite;

            bool isRed();
            bool isContigous(Card card);
    };
}

card.cpp

#include <iostream>
#include "card.h"

using namespace std;
using namespace model;

Card::Card(int face, int suite)
{
    this->face = Face::VALUES[face];
}
Face Card::getFace()
{
}
Suite Card::getSuite()
{
}
bool Card::isMergeable()
{
}
std::string Card::toString()
{
}

bool Card::isRed()
{
}
bool Card::isContigous(Card card)
{
}
Mzdp
  • 43
  • 1
  • 5
  • 7
    Don't try to bring what you know from Java to C++. They are different languages and even similar looking things often have different semantics. – Jesper Juhl Oct 30 '17 at 17:26
  • 1
    C++ provides two different ways to do enumerations at the language level: https://en.wikipedia.org/wiki/Enumerated_type#C.2B.2B – Daniel A. Thompson Oct 30 '17 at 17:28
  • 1
    For your particular error: https://stackoverflow.com/a/4981313/669576 – 001 Oct 30 '17 at 17:30
  • Yes I know, and I have an enum class also I'm just trying and testing better skills on it, but what do you think about then about that https://stackoverflow.com/a/1965344/8857834 – Mzdp Oct 30 '17 at 17:39
  • 1
    Your problem is with `Suite suite;` in the `Card` class. It's wants to call the default ctor here, but you don't provide one. To avoid that, you could, for example, give it a default value: `Suite suite = Suite::Clubs;` (P.S. "DIAMONDS" is spelled with a "d" at the end) – 001 Oct 30 '17 at 18:08
  • 5
    @MCab *I want to use a c++ class like Java's enums* -- Then later on, you may want to start making your own "god" object classes, just like Java. Then your own "finally" just like Java. Then your own "Integer", "Double", etc. class like Java. Then write `instanceof` checking for types like Java, etc. Don't do it -- you will go down a path of code that is hard-to-maintain, bug-ridden, full of memory leaks, and just will look plain weird to a regular C++ programmer. – PaulMcKenzie Oct 30 '17 at 18:20
  • Sorry about the spelling of diamonds, I just typed quick the example and forgot it. I don't want to make my own C++ like Java, but I know that C++ enums are inherited from C and also they are a bit useless to programm OOP properly. – Mzdp Oct 31 '17 at 08:20

1 Answers1

0

Well finally I found a solution.

When we try to implement the 'simulated enum' on the cpp file, we have the this-> pointer and also the scope who envolves it (scope rules), so we need to define the object before the { } of the constructor.

#include <iostream>
#include "card.h"

using namespace std;
using namespace model;

Card::Card(Face face, Suite suite) : face(face), suite(suite)
{

}

That way we could implement a stronger and useful enums with attributes and methods.

Mzdp
  • 43
  • 1
  • 5