SkAshe
SkAshe

Reputation: 3

Read an enum from a header file in a .cpp file (C++)

First off, sorry for my English, I'm a French Canadian.

I have a homework to do: a solitaire game. The problem is that I'm stuck at the obligatory enum step. I need to use an enum for my cards.

Here's the code:

Carte.h:

#ifndef CARTE_H_
#define CARTE_H_

class Carte
{
public:

    enum Sorte
    {
        coeur, pique, carreau, trefle
    };

    enum Valeur
    {
        as, valet, dame, roi
    };

                  Carte             (Sorte& pSorte, Valeur& pValeur);
    Sorte         lireSorte         () const;
    Valeur        lireValeur        () const;
    bool          ecrireSorte       (Sorte& pSorte);
    bool          ecrireValeur      (Valeur& pValeur);
    static bool   validerSorte      (const Sorte& pSorte);
    static bool   validerValeur     (const Valeur& pValeur);

private:

    Sorte  m_sorte;
    Valeur m_valeur;
};
#endif /* CARTE_H_ */

And here's the Carte.cpp:

#include "Carte.h"

using namespace std;

Carte::Carte(Sorte& pSorte, Valeur& pValeur)
{
    m_sorte = pSorte;
    m_valeur = pValeur;
};

Carte::Sorte const lireSorte()
{
    return m_sorte;
};

Valeur const lireValeur()
{
    return m_valeur;
};

bool ecrireSorte(Carte::Sorte& pSorte)
{
    bool donneeValide = validerSorte(pSorte);

    if (donneeValide)
    {
        m_sorte = pSorte;
    }

    return donneeValide;
};

bool ecrireValeur(Valeur& pValeur)
{
    bool donneeValide = validerValeur(pValeur);

    if (donneeValide)
    {
        m_valeur = pValeur;
    }

    return m_valeur;
};

static bool validerSorte(const Sorte& pSorte)
{
    return (pSorte == coeur || pSorte == pique || carreau || trefle);
};

static bool validerValeur(const Valeur& pValeur)
{
    return ((pValeur >= 2 && pValeur <= 10) || (pValeur == as || pValeur == valet || pValeur == dame || pValeur == roi));
};

#endif /* CARTE_H_ */

Everytime that I want to access the private enum(Sorte m_sorte and Valeur m_valeur) in my Carte.cpp, it gives me the following error:

Symbol 'm_valeur' could not be resolved

or

‘carte’ was not declared in this scope

I have really no idea why it's telling me this, I included the .h file so isn't it supposed to know and be able to use my enum? I searched a lot on the internet, and I found people saying that I need to create an object. However, since my Carte.h and Carte.cpp are an object class, how can I create a Carte object my Carte class doesn't even compile?

Thanks and sorry again if I'm unclear!

Upvotes: 0

Views: 104

Answers (1)

StenSoft
StenSoft

Reputation: 9617

The methods in the cpp file are not specified as methods of a class. You need to specify the class name in their definition, e.g.:

Carte::Valeur Carte::lireValeur() const

Some of the returned enum types are not qualified with the class name either, eg. the one fixed in this example was not. Returned types need to be fully qualified becuase the scope of the method (the class they are in) is not yet known; or you can use the alternative function syntax where the return type is specified after the method name so the scope is known:

auto Carte::lireValeur() const -> Valeur

Also fixed in my example is that the const qualifier is for the method (so it comes after the method name), not the return type.

Upvotes: 2

Related Questions