Reputation: 23
I'm trying to create a simple text adventure and I'm splitting the code into multiple headers to make compile time faster and to make it more organized.
//main.cpp
#include <iostream>
#include <fstream>
#include "main_menu.h"
#include "file_select.h"
using namespace std;
int main()
{
main_menu();
}
The Main Menu header file looks like this:
\\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
Finally, the File Select Menu header file and this is where I'm having an error: "main_menu" was not declared in this scope.
#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
Upvotes: 0
Views: 1162
Reputation: 639
Generally, header files should only contain declarations while CPP files should contain the definitions to the declarations. This means that for any declaration, you only provide the return type, function name, and any parameters types followed by a semi-colon. The definition requires that you provide everything in the declaration but also parameter names and all the statements to go inside of the function. Examples are below:
Declaration Example (Header Files):
void file_select();
void main_menu();
int anotherExample(int);
bool finalExample(char optionalVariableName);
Definition Example (CPP Files):
bool finalExample(char optionalVariableName) {
return (optionalVariableName == '\0');
}
That being said, your code should transform into the following:
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.";
}
}
Upvotes: 4