Hello there! i haven't written any c++ code in 3 months, so I'm little rusty. but today I opened an old project and tried to compile it and I got this error:
Undefined reference to 'intro()'
Undefined reference to 'menu(int'
I looked into the code and I can't find any obvious errors, so I googled around and didn't find anything that would fix my error...
Here's my code:
Main.cpp
1 2 3 4 5 6 7 8 9
#include "headers.h"
int main()
{
intro();
menu(0);
}
headers.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14
//all the #includes:
#include <iostream>
#include "windows.h"
#include <cstdlib>
#include <ctime>
#include <string>
// yes using namespace:
usingnamespace std;
//intro + menu located at menu and intro.cpp
void intro();
void menu(int x);
#include "headers.h"
//intro function:: pretty simpple stuff:
void intro()
{
cout << endl;
cout << endl;
cout << endl;
cout << endl;
cout << " A Game By Mrcerimo" << endl;
Sleep(1500);
cout << " The Match the pairs game" << endl;
Sleep(1500);
}
//menu function:
//it contains two kind of menus:
//1.menu is the menu that you get in when you strat the program
//2. menu is the menu you can accsess while playing the game , thus the option to continue::
void menu(int x) // takes in int x, so we will know wich menu to choose:
{
if (x == 0) // first menu
{
int y;
cout << endl;
cout << endl;
cout << endl;
cout << endl;
cout << " MENU" << endl;
cout << endl;
cout << " 1.new game" << endl;
cout << " 2.instructions" << endl;
cout << " 3.exit" << endl;
cout << endl;
cout << "choose a number ";
cin >> y;
switch(y)
{
case 1:
TheGame(); // calls TheGame function localted at the TheGame.cpp
break;
case 2:
Instructions(); // calls Instructions function localted at the Instruction.cpp
break;
case 3:
exit(0); // if you choose number 3 it will exit the program
}
}
else
{
int y;
system("CLS"); // second menu
cout << endl;
cout << endl;
cout << endl;
cout << endl;
cout << " MENU" << endl;
cout << endl;
cout << " 1.new game" << endl;
cout << " 2.instructions" << endl;
cout << " 3.continue" << endl;
cout << " 4.exit" << endl;
cout << endl;
cout << "choose a number ";
cin >> y;
switch(y)
{
case 1:
TheGame(); // calls TheGame function localted at the TheGame.cpp this will start a new game
break;
case 2:
Instructions(); // calls Instructions function localted at the Instruction.cpp
break;
case 3:
return; // if you choose number 4 it will continue the game from the spot what you left, because it goes back where you called this menu function
break;
case 4:
exit(0); // if you choose number 3 it will exit the program
}
}}
and I want ask one question still:
what do you guys think on using the exit(0); as exit on a menu like on my menu above?
Have you compiled all of the cpp files?
It seems to me like the menu_and_intro.cpp file was not supplied to the compiler, so it can't find the definitions for the functions.
If I remember correctly exit() is a C function - so there are better ways of doing this.
I would also not exit the program from another function than main(), this can be very hard to debug in larger scale programs.
In C++ main() is an int, so you can return 0; in main() when you want to exit the program.