gpt4 book ai didi

c++ - 在 C++ 中,如何引用不同头文件中的函数?

转载 作者:行者123 更新时间:2023-11-28 00:07:56 25 4
gpt4 key购买 nike

我正在尝试创建一个简单的文本冒险,并将代码拆分为多个 header ,以加快编译时间并使其更有条理。

//main.cpp
#include <iostream>
#include <fstream>
#include "main_menu.h"
#include "file_select.h"
using namespace std;
int main()
{
main_menu();
}

主菜单头文件如下所示:

\\main_menu.h
#ifndef main_menu_H
#define main_menu_H
#include <iostream>
#include "file_select.h"
using namespace std;
void main_menu() {
cout << string(50,'\n');
cout << " ADVENTURE OF THE SKYLANDS \n";
cout << "================================================================================\n";
cout << " CHAPTER 1 \n";
cout << "================================================================================\n";
cout << " Scepter of The Winds \n";
cout << " v1.0.0 \n";
cout << "================================================================================\n";
char select;
cout << "\n\n\n\n\n" << "MAIN MENU\n\n";
cout << "1. Start\n";
cout << "2. File Select\n";
cout << "3. Credits\n";
cout << "4. Exit Game\n";
cin >> select;
if (select == 'S' || select == 's' || select == '1') {

}
else if (select == 'F' || select == 'f' || select == '2') {
file_select();
}
else if (select == 'C' || select == 'c' || select == '3') {
}
else if (select == 'E' || select == 'e' || select == 'x' || select == 'X' || select == '4') {
return;
}
else {
cout << "Invalid Option!";
}
}
#endif
#include

最后,文件选择菜单头文件,这是我遇到错误的地方:“main_menu”未在此范围内声明。

#ifndef file_select_H
#define file_select_H
#include <iostream>
#include "main_menu.h"
using namespace std;
void file_select() {
cout << " FILE SELECT \n";
cout << "==================================================================================\n";
cout << "1. File 1\n";
cout << "2. File 2\n";
cout << "3. File 3\n";
cout << "4. Return to Main Menu\n";
char fileselect;
cin >> fileselect;
if (fileselect == '1') {
}
else if (fileselect == '2') {
}
else if (fileselect == '3') {
}
else if (fileselect == '4') {
main_menu();
}
else {
cout << "Invalid Option.";
}
}

#endif

最佳答案

通常,头文件应该只包含声明,而 CPP 文件应该包含声明的定义。这意味着对于任何声明,您只需提供返回类型、函数名称和后跟分号的任何参数类型。该定义要求您提供声明中的所有内容,以及参数名称和函数内部的所有语句。示例如下:

声明示例(头文件):

  • void file_select();
  • void main_menu();
  • int anotherExample(int);
  • bool finalExample(char optionalVariableName);

定义示例(CPP 文件):

bool finalExample(char optionalVariableName) {
return (optionalVariableName == '\0');
}

话虽如此,您的代码应转换为以下内容:

main.cpp

#include "MainMenu.h"

using namespace std;

int main() {
main_menu();
return 1;
}

MainMenu.h

#ifndef MAIN_MENU_H
#define MAIN_MENU_H

#ifndef FILE_SELECT_H
#include "FileSelect.h"
#endif

void main_menu();

#endif

MainMenu.cpp

#include <iostream>
#include <string>
#include "MainMenu.h"

using namespace std;

void main_menu() {
cout << string(50,'\n');
cout << " ADVENTURE OF THE SKYLANDS \n";
cout << "================================================================================\n";
cout << " CHAPTER 1 \n";
cout << "================================================================================\n";
cout << " Scepter of The Winds \n";
cout << " v1.0.0 \n";
cout << "================================================================================\n";
char select;
cout << "\n\n\n\n\n" << "MAIN MENU\n\n";
cout << "1. Start\n";
cout << "2. File Select\n";
cout << "3. Credits\n";
cout << "4. Exit Game\n";
cin >> select;
if (select == 'S' || select == 's' || select == '1') {

}
else if (select == 'F' || select == 'f' || select == '2') {
file_select();
}
else if (select == 'C' || select == 'c' || select == '3') {
}
else if (select == 'E' || select == 'e' || select == 'x' || select == 'X' || select == '4') {
return;
}
else {
cout << "Invalid Option!";
}
}

FileSelect.h

#ifndef FILE_SELECT_H
#define FILE_SELECT_H

#ifndef MAIN_MENU_H
#include "MainMenu.h"
#endif

void file_select();

#endif

FileSelect.cpp

#include <iostream>
#include <string>
#include "FileSelect.h"

using namespace std;

void file_select() {
cout << " FILE SELECT \n";
cout << "==================================================================================\n";
cout << "1. File 1\n";
cout << "2. File 2\n";
cout << "3. File 3\n";
cout << "4. Return to Main Menu\n";
char fileselect;
cin >> fileselect;
if (fileselect == '1') {
}
else if (fileselect == '2') {
}
else if (fileselect == '3') {
}
else if (fileselect == '4') {
main_menu();
}
else {
cout << "Invalid Option.";
}
}

关于c++ - 在 C++ 中,如何引用不同头文件中的函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34477822/

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