Search code examples
c++c++11eclipse-marsplaying-cards

Multiple Errors Building Deck of Cards


Receiving two errors trying to compile my files. (Received a down vote without an explanation; I fail to see how this violates any SO guidelines.)

1) c:\mingw\lib\gcc\mingw32\4.9.3\include\c++\bits\c++0x_warning.h:32:2: error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support is currently experimental, and must be enabled with the -std=c++11 or -std=gnu++11 compiler options I attempted to add the flags in Eclipse Mars, but it doesn't seem to have worked. Could that be the only thing i'm missing?

2) \DeckOfCards.h:21:8: error: 'array' in namespace 'std' does not name a template type This is in regards to the std::array<Card,52> cards_; line in my deck of cards header file. This is my first time attempting to build a deck of cards with multiple classes...or a single class for that matter so if i'm making an obvious error I apologize.

EDIT: Exact Message from the Compiler:

07:56:55 **** Incremental Build of configuration Debug for project TexasHoldem ****
Info: Internal Builder is used for build
g++ -O0 -g3 -Wall -c -fmessage-length=0 -o TexasHoldemMain.o "..\\TexasHoldemMain.cpp" 
In file included from c:\mingw\lib\gcc\mingw32\4.9.3\include\c++\array:35:0,
                 from ..\DeckOfCards.h:11,
                 from ..\TexasHoldemMain.cpp:5:
c:\mingw\lib\gcc\mingw32\4.9.3\include\c++\bits\c++0x_warning.h:32:2: error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support is currently experimental, and must be enabled with the -std=c++11 or -std=gnu++11 compiler options.
 #error This file requires compiler and library support for the \
  ^
In file included from ..\TexasHoldemMain.cpp:5:0:
..\DeckOfCards.h:21:8: error: 'array' in namespace 'std' does not name a template type
   std::array<Card,52> cards_;
        ^

07:56:56 Build Finished (took 233ms)

If I recompile a second time after receiving these errors without changing anything I receive a ton more errors everywhere else across multiple files; is that normal?

Main

//Texas Holdem implementation
#include <iostream>

#include "Card.h"
#include "DeckOfCards.h"


int main()
{


    DeckOfCards deck;

    deck.printDeck();

    return(0);
}

DeckOfCards.h

 * DeckOfCards.h
 *
 *  Created on: Jul 8, 2016
 *      Author: 
 */

#ifndef DECKOFCARDS_H_
#define DECKOFCARDS_H_

#include <array>

class DeckOfCards
{

    public:
        DeckOfCards();
        void printDeck();

    private:
        std::array<Card,52> cards_;


};

#endif /* DECKOFCARDS_H_ */

Card.h

* Cards.h
 *
 *  Created on: Jul 8, 2016
 *      Author:
 */

#ifndef CARD_H_
#define CARD_H_

struct Card
{

    enum Suit_Type
    {
        Diamonds,
        Hearts,
        Spades,
        Clubs,
    } suit;

    enum Value_Type
    {

        Two = 2,
        Three = 3,
        Four = 4,
        Five = 5,
        Six = 6,
        Seven = 7,
        Eight = 8,
        Nine = 9,
        Ten = 10,
        Jack = 11,
        Queen = 12,
        King = 13,
        Ace = 14
    } value;

    void printCard();
};

DeckOfCards.cpp

#include <iostream>
#include "DeckOfCards.h"
#include "Card.h"
#include <array>


DeckOfCards::DeckOfCards()
:
    cards_
    {
        {Card::Diamonds, Card::Two},
        {Card::Diamonds, Card::Three},
        {Card::Diamonds, Card::Four},
        {Card::Diamonds, Card::Five},
        {Card::Diamonds, Card::Six},
        {Card::Diamonds, Card::Seven},
        {Card::Diamonds, Card::Eight},
        {Card::Diamonds, Card::Nine},
        {Card::Diamonds, Card::Ten},
        {Card::Diamonds, Card::Jack},
        {Card::Diamonds, Card::Queen},
        {Card::Diamonds, Card::King},
        {Card::Diamonds, Card::Ace},
        {Card::Hearts, Card::Two},
        {Card::Hearts, Card::Three},
        {Card::Hearts, Card::Four},
        {Card::Hearts, Card::Five},
        {Card::Hearts, Card::Six},
        {Card::Hearts, Card::Seven},
        {Card::Hearts, Card::Eight},
        {Card::Hearts, Card::Nine},
        {Card::Hearts, Card::Ten},
        {Card::Hearts, Card::Jack},
        {Card::Hearts, Card::Queen},
        {Card::Hearts, Card::King},
        {Card::Hearts, Card::Ace},
        {Card::Spades, Card::Two},
        {Card::Spades, Card::Three},
        {Card::Spades, Card::Four},
        {Card::Spades, Card::Five},
        {Card::Spades, Card::Six},
        {Card::Spades, Card::Seven},
        {Card::Spades, Card::Eight},
        {Card::Spades, Card::Nine},
        {Card::Spades, Card::Ten},
        {Card::Spades, Card::Jack},
        {Card::Spades, Card::Queen},
        {Card::Spades, Card::King},
        {Card::Spades, Card::Ace},
        {Card::Clubs, Card::Two},
        {Card::Clubs, Card::Three},
        {Card::Clubs, Card::Four},
        {Card::Clubs, Card::Five},
        {Card::Clubs, Card::Six},
        {Card::Clubs, Card::Seven},
        {Card::Clubs, Card::Eight},
        {Card::Clubs, Card::Nine},
        {Card::Clubs, Card::Ten},
        {Card::Clubs, Card::Jack},
        {Card::Clubs, Card::Queen},
        {Card::Clubs, Card::King},
        {Card::Clubs, Card::Ace}
    }
{
}

void DeckOfCards::printDeck()
{

    bool first = true;

    for(auto card : cards_)
    {
        if(!first)
        {

            std::cout << ", ";

        }
        card.printCard();
        first = false;
    }

}

Card.cpp

#include<iostream>
#include "Card.h"
#include "DeckOfCards.h"

Card::Card()
{}

void Card::printCard()
{

    switch(suit)
    {

    case Hearts:
        std::cout << "♥";
        break;
    case Diamonds:
        std::cout << "♦";
        break;
    case Clubs:
        std::cout << "♣";
        break;
    case Spades:
        std::cout << "♠";
        break;
    }
    if(value < Jack)
    {

        std::cout << (int)value;
    }
    else
    {
        switch(value)
        {

        case Jack:
            std::cout << 'J';
            break;
        case Queen:
            std::cout << 'Q';
            break;
        case King:
            std::cout << 'K';
            break;
        case Ace:
            std::cout << 'A';
            break;
        }
    }
}

Solution

  • You have to set -std=c++11 flag for GNU compiler.

    According to Eclipse forums:

    1. Right-click the project and go to "Properties"
    2. Go to: C/C++ Build -> Settings -> Tool Settings -> GCC C++ Compiler -> Miscellaneous -> Other Flags.
    3. Put "-std=c++11" at the end.