构建纸牌的多个错误
Multiple Errors Building Deck of Cards
尝试编译我的文件时收到两个错误。 (在没有解释的情况下收到了反对票;我看不出这违反了任何 SO 准则。)
1) c:\mingw\lib\gcc\mingw32.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
我试图在 Eclipse Mars 中添加标志,但似乎没有用。这可能是我唯一缺少的东西吗?
2) \DeckOfCards.h:21:8: error: 'array' in namespace 'std' does not name a template type
这是关于我的扑克牌头文件中的 std::array<Card,52> cards_;
行。这是我第一次尝试用多个 classes 或单个 class 构建一副纸牌,所以如果我犯了一个明显的错误,我深表歉意。
编辑:来自编译器的确切消息:
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.9.3\include\c++\array:35:0,
from ..\DeckOfCards.h:11,
from ..\TexasHoldemMain.cpp:5:
c:\mingw\lib\gcc\mingw32.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)
如果我在收到这些错误后第二次重新编译而不做任何更改,我会在多个文件的其他地方收到更多错误;这正常吗?
主要
//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;
}
}
}
您必须为 GNU 编译器设置 -std=c++11 标志。
根据 Eclipse 论坛:
- 右键单击该项目并转到 "Properties"
- 转到:
C/C++ 构建 -> 设置 -> 工具设置 -> GCC C++ 编译器 ->
杂项 -> 其他标志。
- 在末尾加上“-std=c++11”。
尝试编译我的文件时收到两个错误。 (在没有解释的情况下收到了反对票;我看不出这违反了任何 SO 准则。)
1) c:\mingw\lib\gcc\mingw32.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
我试图在 Eclipse Mars 中添加标志,但似乎没有用。这可能是我唯一缺少的东西吗?
2) \DeckOfCards.h:21:8: error: 'array' in namespace 'std' does not name a template type
这是关于我的扑克牌头文件中的 std::array<Card,52> cards_;
行。这是我第一次尝试用多个 classes 或单个 class 构建一副纸牌,所以如果我犯了一个明显的错误,我深表歉意。
编辑:来自编译器的确切消息:
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.9.3\include\c++\array:35:0,
from ..\DeckOfCards.h:11,
from ..\TexasHoldemMain.cpp:5:
c:\mingw\lib\gcc\mingw32.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)
如果我在收到这些错误后第二次重新编译而不做任何更改,我会在多个文件的其他地方收到更多错误;这正常吗?
主要
//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;
}
}
}
您必须为 GNU 编译器设置 -std=c++11 标志。
根据 Eclipse 论坛:
- 右键单击该项目并转到 "Properties"
- 转到: C/C++ 构建 -> 设置 -> 工具设置 -> GCC C++ 编译器 -> 杂项 -> 其他标志。
- 在末尾加上“-std=c++11”。