我只是学习来自Java背景的c ++.
现在只是玩简单的类,但出于某种原因,当相同的语法在其他地方编译时,以下将无法编译:
class CardDealer {
private:
string suits[4];
string values[13];
bool cardTaken[4][13];
int getRand(int top);
void getValidSuit(int *suit);
void getValidCard(int suit,int *value);
public:
CardDealer();
string dealCard();
void resetDeck();
};
CardDealer::CardDealer(){
suits = {"hearts", "clubs", "spades", "diamonds"};
values = {"ace","two","three","four","five","six","seven","eight","nine","ten","jack","queen","king"};
cardTaken = {{false,false,false,false,false,false,false,false,false,false,false,false,false},{false,false,false,false,false,false,false,false,false,false,false,false,false},
{false,false,false,false,false,false,false,false,false,false,false,false,false},{false,false,false,false,false,false,false,false,false,false,false,false,false}};
}
Run Code Online (Sandbox Code Playgroud)
显然这只是课程的一部分所以请不要因为错过'}而对我大喊大叫
当编译器遇到构造函数中的实例化时,编译器会摇摆不定,吐出这样的错误:
1>.\CardDealer.cpp(26) : error C2059: syntax error : '{'
1>.\CardDealer.cpp(26) : error C2143: syntax error : missing ';' before '{'
1>.\CardDealer.cpp(26) : error C2143: syntax error : missing ';' before '}'
1>.\CardDealer.cpp(27) : error C2059: syntax error : '{'
1>.\CardDealer.cpp(27) : error C2143: syntax error : missing ';' before '{'
1>.\CardDealer.cpp(27) : error C2143: syntax error : missing ';' before '}'
1>.\CardDealer.cpp(28) : error C2059: syntax error : '{'
Run Code Online (Sandbox Code Playgroud)
第26行是我实例化的西装(suits = {...)
谢谢你看看伙计们,非常感谢
在C++ 0x之前,您只能在声明数组时使用聚合初始化程序语法(即大括号).
请注意,此程序提供了类似的错误:
int thing[4];
int main ()
{
thing = { 0, 1, 2, 3 };
}
Run Code Online (Sandbox Code Playgroud)
你必须使用一些乏味的括号语法初始化你的数组,一次一个元素.