0

I wanted to have an enum class with it's object declared in the header file of my program. I need to modify the state multiple times during the program but it doesn't allow me to. When I run the program it says that there was a multiple definition error regarding the enum class.

I changed the object into static and the program worked (static combinations state;), but in an attempt to assign the state to something else it didn't change the state. In another cpp file I tried modifying the state this way (state = combinations::HIGHCARD)

#ifndef POKER_H_
#define POKER_H_

enum class combinations {
    HIGHCARD, ONEPAIR, TWOPAIRS, THREEOFAKIND, STRAIGHT,
    FLUSH, FULLHOUSE, FOUROFAKIND, STRAIGHTFLUSH, ROYALEFLUSH
};
 combinations state;

void shuffle(int[][13]);
void assign(const char *[], const char *[], int[][13], const char *[], const char *[]);
int deal(const char *[], const char *[], const char *[]);
void printHand(const char *[], const char *[]);
void printWinningMassege(const int, const int, combinations, combinations);
bool isRoyalFlush(const char *[], const char *[], const char *[]);
int isStraightFlush(const char *[], const char *[], const char *[]);
int isFourOfaKind(const char *[], const char *[]);
int isFullHouse(const char *[], const char *[]);
int isFlush(const char *[], const char *[], const char *[]);
int isStraight(const char *[], const char *[]);
int isThreeOfaKind(const char *[], const char *[]);
int isTwoPair(const char *[], const char *[]);
int isPair(const char *[], const char *[]);
int isHighCard(const char *[], const char *[], const char *[]);

#endif // !POKER_H

Severity    Code    Description Project File    Line    Suppression State
Error   LNK2005 "enum combinations state" (?state@@3W4combinations@@A) already defined in Poker.obj PokerDeals  C:\Users\BoB\source\repos\PokerDeals\main.obj   1
Lightness Races in Orbit
  • 369,052
  • 73
  • 620
  • 1,021
Vahan
  • 98
  • 9
  • I believe you want `extern combinations state;`. – Alexander Huszagh Aug 30 '19 at 16:37
  • If you define `combinations state;`, every single source file that includes it will have a copy, giving you the "already defined error". If you define it in a single source file, use `extern combinations state;` in the header, and then modify it in other source files, you should be fine. – Alexander Huszagh Aug 30 '19 at 16:39
  • I've tried it, it gives me an error like this Error LNK2001 unresolved external symbol "enum combinations state" (?state@@3W4combinations@@A) – Vahan Aug 30 '19 at 16:40
  • 1
    What you must do is write `extern combinations state;` in `poker.h` and `combinations state;` in exactly one `.cpp` file. – Paul Sanders Aug 30 '19 at 16:43
  • This is just "where do I put globals". Has nothing to do with enums or with any of those functions – Lightness Races in Orbit Aug 30 '19 at 16:48
  • 1
    Oh, I just didn't know how to declare an extern object. It works fine now. Thank you everyone for the answers. – Vahan Aug 30 '19 at 16:48

0 Answers0