1

I got a problem when I want to pass an enum value to a default construtor. My enums are defined like this:

typedef enum
{
    DOUBLOON,
    VICTORYPOINT
} ENUMchipType;

They are stored in a separate .h file.

But when i try to do this:

chips m_doubloon(DOUBLOON);

I get the following error:

error: C2061: syntax error : identifier 'DOUBLOON'

The code for the default constructor is:

chips::chips(
    ENUMchipType chipType = DOUBLOON,
    int amountValue1 = 0,
    int amountValue5 = 0,
    QObject *parent = 0) :
    m_chipType(chipType),
    m_chipCountValue1(amountValue1),
    m_chipCountValue5(amountValue5),
    QObject(parent) {}

Anyone an idea what is wrong with this piece of code? Thanks in advance!

Edit: I already tried putting the enum is a class als a public member and derive the chips class from it, but without any succes.

EDIT 2: This piece of code reproduces the error in Visual Studio 2013

#include <string>

using namespace std;

//enums.h
typedef enum
{
    DOUBLOON,
    VICTORYPOINT
} ENUMchipType;

typedef enum
{
    PLAYER1,
    PLAYER2,
    PLAYER3,
    PLAYER4,
    PLAYER5
} ENUMplayer;

// In chips.h
class chips
{
private:
    int m_chipCountValue5;
    int m_chipCountValue1;
    ENUMchipType m_chipType;

public:
    explicit chips(
        ENUMchipType chipType = ENUMchipType::DOUBLOON,
        int amountValue1 = 0,
        int amountValue5 = 0);

    ENUMchipType getChipType() const { return m_chipType; }
};

// Chips.cpp
chips::chips(ENUMchipType chipType, int amountValue1, int amountValue5) :
m_chipType(chipType),
m_chipCountValue1(amountValue1),
m_chipCountValue5(amountValue5) {}

// PLayer.h
class player
{
private:
    ENUMplayer m_ID;
    string m_name;

public:
    chips m_doubloon(DOUBLOON);
    chips m_victoryPoints(VICTORYPOINT);

    explicit player(ENUMplayer ID = PLAYER1, string name = "");

    void setName(string name = "") { m_name = name; }
    void setID(ENUMplayer ID) { m_ID = ID; }

    string getName() const { return m_name; }
    ENUMplayer getID() const { return m_ID; }

};

//player.cpp
player::player(ENUMplayer ID, string name) :
m_ID(ID),
m_name(name) {}

int main() {

    return 0;
}
Yoshi Peters
  • 196
  • 1
  • 2
  • 13
  • Does it work if you don't give anything to the constructor (chips m_doubloon{};)? – KABoissonneault May 25 '15 at 12:01
  • Yes @KABoissonneault, it uses the default values than. – Yoshi Peters May 25 '15 at 12:02
  • 3
    Have you included the header that declares the enumeration? Is the enumeration in an accessible namespace? Please post [enough code](http://stackoverflow.com/help/mcve) to demonstrate the error. – Mike Seymour May 25 '15 at 12:04
  • @MikeSeymour Yes I have, but what do you mean with the enum being in an accessible namespace? – Yoshi Peters May 25 '15 at 12:06
  • @YoshiPeters: Did you declare the enumeration in a namespace? Or a class? Or somewhere else? Please post [enough code](http://stackoverflow.com/help/mcve) to demonstrate the error. – Mike Seymour May 25 '15 at 12:06
  • @Yoshi Peters For example, is it defined in a private section of a class? Or defined in a class at all? For example, if you defined the enum in chips, you'd have to use chips::DOUBLOON – KABoissonneault May 25 '15 at 12:07
  • @MikeSeymour globally, in a separate header file, i tried putting it in a class like here: http://stackoverflow.com/questions/2870301/c-pass-enum-as-parameter but without any succes – Yoshi Peters May 25 '15 at 12:08
  • Maybe you could try defining the enum in a less "C" way and more in a C++ way. enum class ENUMchipType { ... }; instead of typedef enum { ... } ENUMchipType – KABoissonneault May 25 '15 at 12:11
  • Is your chips class seperated into .h and .cpp? It looks like you've got default parameters in the .cpp implementation. And that would be wrong. Is your #include in the .h? – acraig5075 May 25 '15 at 12:11
  • @YoshiPeters: So it's in the global namespace, and you're absolutely sure you're including the header, before you try to use the name `DOUBLOON`? In that case, something else is wrong, and we can't guess what it might be from the code you've posted. Does the header have an include guard? Have you made sure that's not the same as another header's? Please post [enough code](http://stackoverflow.com/help/mcve) to demonstrate the problem. – Mike Seymour May 25 '15 at 12:11
  • @acraig5075 Yes I have, I just put it together to keep the post small. – Yoshi Peters May 25 '15 at 12:12
  • @MikeSeymour Header is included, with #ifndef include guards. I'm gonna check for a better piece of code. – Yoshi Peters May 25 '15 at 12:14
  • @YoshiPeters: Unfortunately, you've made it too small, so there's not enough information to reproduce the error. I don't get any error if I assemble these scaps of code into something I can compile: http://ideone.com/9MIHME – Mike Seymour May 25 '15 at 12:15
  • @MikeSeymour I put a piece of code in de post that gives me the error. – Yoshi Peters May 25 '15 at 12:30

3 Answers3

2

In class player, you should replace

chips m_doubloon(DOUBLOON);
chips m_victoryPoints(VICTORYPOINT);

by

chips m_doubloon{DOUBLOON};
chips m_victoryPoints{VICTORYPOINT};
Jarod42
  • 203,559
  • 14
  • 181
  • 302
2

Now you've finally posted enough code, we see that this

chips m_doubloon(DOUBLOON);

is actually a class member declaration. Class members can't be initialised with (), only with = or {}. Assuming your compiler supports in-class initialisation (introduced in C++11), you should be fine with

chips m_doubloon{DOUBLOON};
                ^        ^

Alternatively, you could initialise the members in the constructor's initialiser list rather than in their declarations.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
  • Yes that was the problem! thank you! – Yoshi Peters May 25 '15 at 12:35
  • I was thinking it was a construtor for the class, and using the () I was calling the constructor for that class. Am i wrong with thinking this? – Yoshi Peters May 25 '15 at 12:49
  • 1
    @YoshiPeters: It's a member declaration, not a constructor. Unlike declarations in other places, you can't use `()` to provide an initialiser or constructor arguments for a member declaration. – Mike Seymour May 25 '15 at 12:51
-1

You need to pass DOUBLOON as ENUMchipType::DOUBLOON

Tamás Szabó
  • 733
  • 6
  • 9