如何在C ++中使用函数函数 [英] How can I use function in function in C++
问题描述
#include <iostream>
#include <string>
using namespace std;
void play();
void startgame();
int main()
{
string wantToPlay;
int counts[]={0,0,0};
int timesPlayed;
int userScore;
int compScore;
{
play();
}
void startgame()
{
cout >>"Want to play ? Y or N";
cin <<wantToPlay;
while(wantToPlay=="Y"||wantToPlay=="y")
{
play();
}
}
void play()
{
int userChoice=0;
int userFav=-1;
int moves[]={1,2,0};
cout <<"Your Move ?"<<endl;
cout <<"0 : Stone"<<endl;
cout <<"1 : Paper"<<endl;
cout <<"2 : Scissor"<<endl;
cin >>userChoice;
counts[userChoice]++;
int compChoice = 0;
int max = -1;
if (timesPlayed == 0)
{
compChoice = static_cast<int>(std::floor(Math::random() * 3));
}
else
{
for (int i = 0;i < counts.size();i++)
{
if (counts[i] > max)
{
max = counts[i];
userFav = i;
}
}
compChoice = moves[userFav];
}
std::wcout << std::wstring(L"Computer's Move : ") << compChoice << std::endl;
int result = userChoice - compChoice;
if (result == 1 || result == -2)
{
userScore++;
std::wcout << std::wstring(L"You Win") << std::endl;
}
else if (result == 0)
{
std::wcout << std::wstring(L"Game Tied") << std::endl;
}
else
{
compScore++;
std::wcout << std::wstring(L"You Lose") << std::endl;
}
std::wcout << std::wstring(L"You ") << userScore << std::wstring(L" - ") << compScore << std::wstring(L" Computer") << std::endl;
timesPlayed = 1;
startGame();
}
}
我的尝试: < br $>
错误:在'{'令牌之前不允许使用函数定义//这是我正在犯的错误
What I have tried:
error: a function-definition is not allowed here before '{' token //THIS IS THE ERROR I AM GETTING
推荐答案
错误:在'{'token
error: a function-definition is not allowed here before '{' token
C ++不允许在另一个函数中定义函数。
以下是语言作者对C和C ++参考书的链接。注意,C是C ++的祖先,所以知道C对C ++总是有用。
C编程语言 - 维基百科,免费的百科全书 [ ^ ]
https://hassanolity.files.wordpress.com/2013/11/the_c_programming_language_2。 pdf [ ^ ]
http://www.ime.usp。 br / ~pf / Kernighan-Ritchie / C-Programming-Ebook.pdf [ ^ ]
C ++编程语言 [ ^ ]
C++ do not allow to define a function inside another function.
Here is links to references books on C and C++ by the authors of the languages. Note than C is the ancestor of C++, so knowing C is always useful with C++.
The C Programming Language - Wikipedia, the free encyclopedia[^]
https://hassanolity.files.wordpress.com/2013/11/the_c_programming_language_2.pdf[^]
http://www.ime.usp.br/~pf/Kernighan-Ritchie/C-Programming-Ebook.pdf[^]
C++ Programing Language[^]
这篇关于如何在C ++中使用函数函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!