Undefined reference to error

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:
using namespace std;

//intro + menu located at menu and intro.cpp
void intro();
void menu(int x);


menu and intro.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
   #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?

thanks in advance=)
Last edited on
closed account (o3hC5Di1)
Hi there,

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.

Hope that helps.

All the best,
NwN
NwN you were right, the menu_and_intro.cpp file was not supplied to the compiler...

Thanks a lot for quick respond!
closed account (o3hC5Di1)
Most welcome, glad I could help.

Have a nice day,
NwN
Topic archived. No new replies allowed.