找不到标识符错误?
Identifier not found error?
我正在编写代码以使用 C++ 制作基于文本的基本二十一点游戏。我刚开始,但我立即 运行 遇到了一个问题。这是我到目前为止的代码,请注意,它并不意味着完整,甚至 运行。
是的,我知道它非常混乱和低效,我在我的第一个 C++ class 中。
我的问题是,每当我的代码调用任何递归函数(DealingHandler 和 EndProgram)时,我都会得到 "identifier not found"。
是的,我知道有一个专门结束程序的功能在这个程序中是完全没有用的,但我不知道怎么突然结束程序没有这样的东西。
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std; const int maxScore = 21; //immutable upper limit
//string nameInput; - Idea for later.
int HandVal, DealerHandVal, Aceone, val, aceVal;
char HitStay;
string DealerHand, currenthand; //Strings to output the cards ( 4 5 for example)
int numAces, numTwo, numThree, numFour, numFive, numSix, numSeven, numEight, numNine, numTen, numJack, numQueen, numKing; //Inefficient way of making sure it doesn't give five twos out, however unlikely that may be.
int main() {
cout << "Welcome to our C++ BlackJack Program." << endl;
cout << "Please press enter to begin the game." << endl;
system("PAUSE");//Bad. Still easiest for a beginning C++ class.
currenthand = "";
numAces = 4;
numTwo = 4;
numThree = 4;
numFour = 4;
numFive = 4;
numSix = 4;
numSeven = 4;
numEight = 4;
numNine = 4;
numTen = 4;
numJack = 4;
numQueen = 4;
numKing = 4;
int i = 2;
{
DealingHandler(i);
cout << DealerHand;
cout << "Press H to hit or S to stay." << endl;
cin >> HitStay;
switch (HitStay){
case 'H':
DealingHandler(1);
break;
case 'h':
DealingHandler(1);
break;
case 's':
EndProgram();
break;
case 'S':
EndProgram();
break;
default:
cout << "Invalid entry.";
break;
}
}
system("PAUSE");
}
int DealingHandler(int HowManyDealed) {
for (int x = 0; x <= HowManyDealed; x++) {
val = rand() % 13;
switch (val) {
case '1':
cout << "A";
cout << "Press One to set this Ace equal to one. Press Two to set it equal to eleven. This cannot be changed, so choose wisely!" << endl;
cin >> aceVal;
if (aceVal = 1) {
Aceone = 1;
currenthand += " 1";
cout << currenthand << endl;
HandVal += 1;
}
if (aceVal = 2) {
Aceone = 11;
currenthand += " 11";
cout << currenthand << endl;
HandVal += 11;
}
numAces = numAces - 1;
case '2':
currenthand += " 2";
cout << currenthand << endl;
HandVal += 3;
numTwo -= 1;
break;
case '3':
currenthand += " 3";
cout << currenthand << endl;
HandVal += 3;
numThree -= 1;
break;
case '4':
currenthand += " 4";
cout << currenthand << endl;
HandVal += 4;
numFour -= 1;
break;
case '5':
currenthand += " 5";
cout << currenthand << endl;
HandVal += 5;
numFive -= 1;
break;
case '6':
currenthand += " 6";
cout << currenthand << endl;
HandVal += 6;
numSix -= 1;
break;
case '7':
currenthand += " 7";
cout << currenthand << endl;
HandVal += 7;
numSeven -= 1;
break;
case '8':
currenthand += " 8";
cout << currenthand << endl;
HandVal += 8;
numEight -= 1;
break;
case '9':
currenthand += " 9";
cout << currenthand << endl;
HandVal += 9;
numNine -= 1;
break;
case '10':
currenthand += " 10";
cout << currenthand << endl;
HandVal += 10;
numTen -= 1;
break;
case '11':
currenthand += " J";
cout << currenthand << endl;
HandVal += 10;
numJack -= 1;
break;
case '12':
currenthand += " Q";
cout << currenthand << endl;
HandVal += 10;
numQueen -= 1;
break;
case '13':
currenthand += " K";
cout << currenthand << endl;
HandVal += 10;
numKing -= 1;
break;
}
if (HandVal < 21) {
cout << "Busted!" << endl;
EndProgram();
}
}
return HandVal;
}
void EndProgram()
{
cout << "I hope you had fun with my game!";
system("PAUSE");
}
在此先感谢您对我的帮助。
This 是声明、原型和定义的一个很好的例子,这些都是您需要知道的。
函数原型是一种声明函数结构体的方法,例如:
int my_function(int arg1, int arg2);
事实上,在函数原型中你甚至不需要输入参数名称,只需要输入它们的类型:
int my_function(int, int);
随着编译过程从上到下,当你有这段代码时
// function a definition
int function_a()
{
int some_variable = function_b();
}
// function b definition
int function_b()
{
return 20;
}
当您在 function_a() 中调用编译器时,编译器将对 function_b() 一无所知,因此,您必须像这样重新排序代码
// function b definition
int function_b()
{
return 20;
}
// function a definition
int function_a()
{
// now the compiler knows how function_b() works, or, at least, what arguments she need
int some_variable = function_b();
}
或使用原型来保留您的代码组织,就像这样
// function b prototype
int function_b();
// function a definition
int function_a()
{
int some_variable = function_b();
}
// function b definition
int function_b()
{
return 20;
}
所以,现在,编译器知道存在一个 "function_b",并且知道她的参数和 return 值类型是什么(在这种情况下,没有参数和 return 类型是 int)
为了顺序,您通常会将函数原型放在头文件(.h)中,将定义放在代码文件中(.c/cpp)
在您的示例中,您在主函数内部调用了 DealingHandler 和 EndProgram 函数,但它们是稍后定义的,这就是您收到错误的原因。所以,使用函数原型你可以很容易地修复它,把
/* this can be
int DealingHandler(int);
because function prototypes doesn't need argument's name, just their types */
int DealingHandler(int HowManyDealed);
void EndProgram();
在 main() 函数上,或者更好的是,在这一行下面
using namespace std;
我正在编写代码以使用 C++ 制作基于文本的基本二十一点游戏。我刚开始,但我立即 运行 遇到了一个问题。这是我到目前为止的代码,请注意,它并不意味着完整,甚至 运行。 是的,我知道它非常混乱和低效,我在我的第一个 C++ class 中。
我的问题是,每当我的代码调用任何递归函数(DealingHandler 和 EndProgram)时,我都会得到 "identifier not found"。 是的,我知道有一个专门结束程序的功能在这个程序中是完全没有用的,但我不知道怎么突然结束程序没有这样的东西。
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std; const int maxScore = 21; //immutable upper limit
//string nameInput; - Idea for later.
int HandVal, DealerHandVal, Aceone, val, aceVal;
char HitStay;
string DealerHand, currenthand; //Strings to output the cards ( 4 5 for example)
int numAces, numTwo, numThree, numFour, numFive, numSix, numSeven, numEight, numNine, numTen, numJack, numQueen, numKing; //Inefficient way of making sure it doesn't give five twos out, however unlikely that may be.
int main() {
cout << "Welcome to our C++ BlackJack Program." << endl;
cout << "Please press enter to begin the game." << endl;
system("PAUSE");//Bad. Still easiest for a beginning C++ class.
currenthand = "";
numAces = 4;
numTwo = 4;
numThree = 4;
numFour = 4;
numFive = 4;
numSix = 4;
numSeven = 4;
numEight = 4;
numNine = 4;
numTen = 4;
numJack = 4;
numQueen = 4;
numKing = 4;
int i = 2;
{
DealingHandler(i);
cout << DealerHand;
cout << "Press H to hit or S to stay." << endl;
cin >> HitStay;
switch (HitStay){
case 'H':
DealingHandler(1);
break;
case 'h':
DealingHandler(1);
break;
case 's':
EndProgram();
break;
case 'S':
EndProgram();
break;
default:
cout << "Invalid entry.";
break;
}
}
system("PAUSE");
}
int DealingHandler(int HowManyDealed) {
for (int x = 0; x <= HowManyDealed; x++) {
val = rand() % 13;
switch (val) {
case '1':
cout << "A";
cout << "Press One to set this Ace equal to one. Press Two to set it equal to eleven. This cannot be changed, so choose wisely!" << endl;
cin >> aceVal;
if (aceVal = 1) {
Aceone = 1;
currenthand += " 1";
cout << currenthand << endl;
HandVal += 1;
}
if (aceVal = 2) {
Aceone = 11;
currenthand += " 11";
cout << currenthand << endl;
HandVal += 11;
}
numAces = numAces - 1;
case '2':
currenthand += " 2";
cout << currenthand << endl;
HandVal += 3;
numTwo -= 1;
break;
case '3':
currenthand += " 3";
cout << currenthand << endl;
HandVal += 3;
numThree -= 1;
break;
case '4':
currenthand += " 4";
cout << currenthand << endl;
HandVal += 4;
numFour -= 1;
break;
case '5':
currenthand += " 5";
cout << currenthand << endl;
HandVal += 5;
numFive -= 1;
break;
case '6':
currenthand += " 6";
cout << currenthand << endl;
HandVal += 6;
numSix -= 1;
break;
case '7':
currenthand += " 7";
cout << currenthand << endl;
HandVal += 7;
numSeven -= 1;
break;
case '8':
currenthand += " 8";
cout << currenthand << endl;
HandVal += 8;
numEight -= 1;
break;
case '9':
currenthand += " 9";
cout << currenthand << endl;
HandVal += 9;
numNine -= 1;
break;
case '10':
currenthand += " 10";
cout << currenthand << endl;
HandVal += 10;
numTen -= 1;
break;
case '11':
currenthand += " J";
cout << currenthand << endl;
HandVal += 10;
numJack -= 1;
break;
case '12':
currenthand += " Q";
cout << currenthand << endl;
HandVal += 10;
numQueen -= 1;
break;
case '13':
currenthand += " K";
cout << currenthand << endl;
HandVal += 10;
numKing -= 1;
break;
}
if (HandVal < 21) {
cout << "Busted!" << endl;
EndProgram();
}
}
return HandVal;
}
void EndProgram()
{
cout << "I hope you had fun with my game!";
system("PAUSE");
}
在此先感谢您对我的帮助。
This 是声明、原型和定义的一个很好的例子,这些都是您需要知道的。
函数原型是一种声明函数结构体的方法,例如:
int my_function(int arg1, int arg2);
事实上,在函数原型中你甚至不需要输入参数名称,只需要输入它们的类型:
int my_function(int, int);
随着编译过程从上到下,当你有这段代码时
// function a definition
int function_a()
{
int some_variable = function_b();
}
// function b definition
int function_b()
{
return 20;
}
当您在 function_a() 中调用编译器时,编译器将对 function_b() 一无所知,因此,您必须像这样重新排序代码
// function b definition
int function_b()
{
return 20;
}
// function a definition
int function_a()
{
// now the compiler knows how function_b() works, or, at least, what arguments she need
int some_variable = function_b();
}
或使用原型来保留您的代码组织,就像这样
// function b prototype
int function_b();
// function a definition
int function_a()
{
int some_variable = function_b();
}
// function b definition
int function_b()
{
return 20;
}
所以,现在,编译器知道存在一个 "function_b",并且知道她的参数和 return 值类型是什么(在这种情况下,没有参数和 return 类型是 int)
为了顺序,您通常会将函数原型放在头文件(.h)中,将定义放在代码文件中(.c/cpp)
在您的示例中,您在主函数内部调用了 DealingHandler 和 EndProgram 函数,但它们是稍后定义的,这就是您收到错误的原因。所以,使用函数原型你可以很容易地修复它,把
/* this can be
int DealingHandler(int);
because function prototypes doesn't need argument's name, just their types */
int DealingHandler(int HowManyDealed);
void EndProgram();
在 main() 函数上,或者更好的是,在这一行下面
using namespace std;