undefined reference to `foo'

closed account (1v5E3TCk)
I am making a text-based RPG game or you can call it as an engine.
But I have a problem. When I try to compile my project it gives errors like that.

You can find all files here: https://github.com/senhor/WoDK/

Error giving file:
combat.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
  #include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include "C:\Users\-exper-\Desktop\proje\WoDK\globals.h"

void combat()
{
    srand(time(NULL));

    enemy current[ 3 ];

    for( int i = 0; i<3; ++i)
    {
        if( foo->get_level() > 7 )
        {
            int x = ( rand()%3 ) + 7;
            current[ i ] = enemies[ x ];
        }
        else
        {
            int x = ( rand()%3 ) + foo->get_level();
            current[ i ] = enemies[ x ];
        }



    }

    std::cout<< "You attacked by a " << current[ 0 ].get_name() << ", a " << current[ 1 ].get_name() << ", a " << current[ 2 ].get_name();

    while( ( current[ 0 ].get_current_hp() > 0 || current[ 1 ].get_current_hp() > 0 || current[ 2 ].get_current_hp() > 0 ) && foo->get_current_hp() > 0 )
    {
        int choose = 0;

        for( int i = 0; i < 3; ++i)
        {
            std::cout<< std::endl << i << "." << current[ i ].get_name() << "        HP:" << current[ i ].get_current_hp() << "/" << current[ i ].get_maximum_hp() << "           MP:" << current[ i ].get_current_mp() << "/" << current[ i ].get_maximum_mp();
        }

        std::cout<< std::endl << "P." << foo->get_name() << "        HP:" << foo->get_current_hp() << "/" << foo->get_maximum_hp() << "        MP:" << foo->get_current_mp() << "/" << foo->get_maximum_mp();

        std::cout<< "\nWhich enemy do you want to attack?";
        std::cout<< "\n1." << current[ 0 ].get_name();
        std::cout<< "\n2." << current[ 1 ].get_name();
        std::cout<< "\n3." << current[ 2 ].get_name();
        std::cout<< "\n4.Use Pot";

        std::cin>> choose;
        --choose;

        if( choose >= 0 && choose < 3 )
        {
            ///attack selection;
            int attack;
            std::cout<< "\n1.Normal Attack        2.Magic Attack        3.Back";
            std::cin >> attack;

            if( attack == 1 )
            {
                current[ choose ].decrease_current_hp( foo->get_attack() );

                if( current[ choose ].get_current_hp() <= 0 )
                {
                    std::cout<< "\nYou have killed " << current[ choose ].get_name();
                    foo->increase_exp( current[ choose ].get_exp() );
                    foo->increase_gold( current[ choose ].get_gold() );
                    if( foo->if_level_up() )
                    {
                        foo->level_up(); ///not done yet.
                    }
                }
            }
            if( attack == 2 )
            {
                int bar;

                for( int i = 0; i < 4; ++i )
                {
                    if( foo->if_skill_learned( i ) )
                    {
                        std::cout<< std::endl << i << ". " << foo->get_skill_name( i );
                    }
                }

                std::cout<< "4.Back";

                std::cin>> bar;

                if( bar >= 0 && bar < 4)
                {
                    std::cout<< std::endl << foo->get_skill_explanation( bar );
                    current[ choose ].decrease_current_hp( foo->get_magic_attack( bar ) );
                    foo->decrease_current_mp( foo->get_skill_mp( bar ) );

                    if( current[ choose ].get_current_hp() <= 0 )
                    {
                        std::cout<< "\nYou have killed " << current[ choose ].get_name();
                        foo->increase_exp( current[ choose ].get_exp() );
                        foo->increase_gold( current[ choose ].get_gold() );
                        if( foo->if_level_up() )
                        {
                            foo->level_up(); ///not done yet.
                        }
                    }
                }

                if( bar == 5 )
                {
                    continue;
                }
            }
            if( attack == 3 )
            {
                continue;
            }

        }
        if( choose == 3 )
        {
            ///pot using

            int pot;

            for( int i = 0; i<10; ++i )
            {
                std::cout<< std::endl << i << "." << pots[ i ].get_name();
            }
            std::cout<< std::endl << "10.Back";

            std::cin >> pot;

            if( pot >= 0 && pot < 10 && foo->if_have_pot( pot ) )
            {
                foo->increase_current_hp( pots[ pot ].get_hp() );
                foo->increase_current_mp( pots[ pot ].get_mp() );
                foo->decrease_pot( pot );
            }

            if( pot == 10 )
            {
                continue;
            }

            else if( !foo->if_have_pot( pot ) )
            {
                std::cout << "\nYou dont have that potion";
            }
        }

///        do
///        {
          ///  foo.loop();  ///We dont need that until adding skill effects
            current[ 0 ].loop(); ///    /
            current[ 1 ].loop(); ///   < we need them because they are basic AI.
            current[ 2 ].loop(); ///
///        } while();

    }
}
Since you didn't post globals.h, it's impossible to tell if or how you've defined foo.

You reference foo at line 15, so if you're not getting compile errors, I'm presuming globals.h contains the following:

1
2
 
  extern Foo * foo;  // says foo exists in another module 

Since you state the linker is telling you that foo is undefined, then you have not included foo anywhere. One module needs to have foo defined without the extern.

closed account (1v5E3TCk)
You can find all files att github but

globals.h:
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
class player;
class place;

#ifndef GLOBALS_H_INCLUDED
#define GLOBALS_H_INCLUDED

#include "C:\Users\-exper-\Desktop\proje\WoDK\Player\player.h"
#include "C:\Users\-exper-\Desktop\proje\WoDK\Object\object.h"
#include "C:\Users\-exper-\Desktop\proje\WoDK\Enemy\enemy.h"
#include "C:\Users\-exper-\Desktop\proje\WoDK\Place\place.h"

extern player* pets;
extern player* foo;

extern enemy enemies[ 20 ];

extern object empty;
extern object pots[ 10 ];
extern object warrior_armors[ 10 ];
extern object warrior_weapons[ 10 ];
extern object warrior_shields[ 10 ];

extern object wizzard_armors[ 10 ];
extern object wizzard_weapons[ 10 ];
extern object wizzard_shields[ 10 ];


#endif // GLOBALS_H_INCLUDED


and

globals.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
#include "globals.h"

void global_defination()
{
    ///defines here;
    pets = new player[ 10 ];
    foo = new player;

    foo->create();

    empty.set_level( 0 );
    empty.set_hp( 0 );
    empty.set_maximum_attack( 0 );
    empty.set_minimum_attack( 0 );
    empty.set_mp( 0 );
    empty.set_name( "empty" );
    empty.set_price( 0 );
    empty.set_type( potion );

    warrior_armors[ 0 ].set_hp( 2 );
    warrior_armors[ 0 ].set_level( 0 );
    warrior_armors[ 0 ].set_maximum_attack( 0 );
    warrior_armors[ 0 ].set_minimum_attack( 0 );
    warrior_armors[ 0 ].set_mp( 0 );
    warrior_armors[ 0 ].set_name( "Broken Sword" );
    warrior_armors[ 0 ].set_price( 0 );
    warrior_armors[ 0 ].set_type( weapon );

}
At line 2 of globals.cpp, add:
1
2
 
  player * foo = NULL; 



closed account (1v5E3TCk)
You are my hero :) Thanks a lot.

Corrected globals.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
#include "globals.h"

player* foo = NULL;
player* pets = NULL;

enemy enemies[ 20 ];

object empty;
object pots[ 10 ];
object warrior_armors[ 10 ];
object warrior_weapons[ 10 ];
object warrior_shields[ 10 ];

object wizzard_armors[ 10 ];
object wizzard_weapons[ 10 ];
object wizzard_shields[ 10 ];

void global_defination()
{
    ///defines here;
    pets = new player[ 10 ];
    foo = new player;

    foo->create();

    empty.set_level( 0 );
    empty.set_hp( 0 );
    empty.set_maximum_attack( 0 );
    empty.set_minimum_attack( 0 );
    empty.set_mp( 0 );
    empty.set_name( "empty" );
    empty.set_price( 0 );
    empty.set_type( potion );

    warrior_armors[ 0 ].set_hp( 2 );
    warrior_armors[ 0 ].set_level( 0 );
    warrior_armors[ 0 ].set_maximum_attack( 0 );
    warrior_armors[ 0 ].set_minimum_attack( 0 );
    warrior_armors[ 0 ].set_mp( 0 );
    warrior_armors[ 0 ].set_name( "Broken Sword" );
    warrior_armors[ 0 ].set_price( 0 );
    warrior_armors[ 0 ].set_type( weapon );

}
Last edited on
Topic archived. No new replies allowed.