gpt4 book ai didi

C++ 我无法访问我需要的功能

转载 作者:行者123 更新时间:2023-11-28 06:52:52 26 4
gpt4 key购买 nike

我正在尝试用 C++ 为我的学校项目创建一个基于文本的冒险游戏。我遇到的问题是我的 gameover() 函数需要能够转到我的 begin() 函数。问题是必须在 gameover() 函数之前声明 begin 以允许它转到 begin(),只有我有其他函数也需要访问 gameover(。简而言之,我需要一种方法来告诉我的程序转到函数 gameover() 或 begin(),并知道它存在并已声明。谢谢,西蒙

void begin() {
int name;
int choice1;
system("cls");
cout << "To start your adventure, please enter your name." << endl;
cin >> name;
cin.ignore();
system("cls");
cout << "Hello " << name << " Your adventure now begins.... Who knows what's in store for you!" << endl;
system("pause");
system("cls");
cout << "You find yourself in a dark, cramp library. " << endl;
cout << "You don't know how you got here, or where you are." << endl;
cout << "Luckily there is a sword laying on the ground next to you \nand an open door in front.\n" << endl;
cout << "What will you do?" << endl;
cout << "1. Pick up the sword" << endl;
cout << "2. Walk forward through the door" << endl;
cout << "3. Use the sword to end your miserable existence!" << endl;
cin >> choice1;
cin.ignore();
system("cls");
if (choice1 == 1) {
cout << "You quickly pick up the sword and run through the door." << endl;
system("pause");
road();
}
else if (choice1 == 2) {
cout << "While you make you way to the door...." << endl;
cout << "You somehow managed to trip on the sword." << endl;
cout << "You fall on the hilt smashing your neck, and end your painfully short life. " << endl;
system("pause");
gameover();
}
else {
cout << "That wasn't an option....." << endl;
cout << "You have now broken the game. Good day Sir!!!" << endl;
}
}


void gameover() {
int choice_b;
cout << " Oops! You died.... Try Again." << endl;
cout << "\n1. Start Again!" << endl;
cout << "2. Exit" << endl;

cin >> choice_b;
cin.ignore();
system("cls");

if (choice_b == 1) {
begin();
}
else if (choice_b == 2) { std::exit; }
}

最佳答案

C++ 要求您在调用语句之前描述函数。如果您要在 main() 顶部添加定义,那么它的调用语句将在任何地方工作,第二个选项是在调用之前声明函数。由您决定要在何处访问该功能。

基本上,如果您在头文件或 main 顶部添加一些声明,那么这些函数将在任何地方运行

#include headerfiles....
void begin(); //
void end(); // Function prototypes
int main()
{
.....
begin(); // Will work here
}

关于C++ 我无法访问我需要的功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23589760/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com