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

时间:2015-12-27 05:19:07

标签: c++ file text

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

//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

1 个答案:

答案 0 :(得分:3)

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

声明示例(页眉文件):

  • ProjectionOperation operation = new ProjectionOperation (); operation.andInclude((String[])map.values().toArray()); agg = newAggregation(operation, group((String[])map.values().toArray()));
  • void file_select();
  • void main_menu();
  • int anotherExample(int);

定义示例(CPP文件):

bool finalExample(char optionalVariableName);

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

  

的main.cpp

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

MainMenu.h

#include "MainMenu.h"

using namespace std;

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

MainMenu.cpp

#ifndef MAIN_MENU_H
#define MAIN_MENU_H

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

void main_menu();

#endif
  

FileSelect.h

#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.cpp

#ifndef FILE_SELECT_H
#define FILE_SELECT_H

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

void file_select();

#endif