0

I was coding a simple text based RPG when I came across this error. I use the Code::Blocks IDE, so it should compile it all for me. I'm new to C++ so I couldn't make sense out of all the rather advanced codes getting this same error. Here's the function it occurred in (home_action.cpp):

#include <iostream>
#include "header.h"

using namespace std;

void home_action()
{
    cout << "";
    cout << "\nYour health:  " << health;
    cout << "\nYour mana:  " << magic;
    cout << "\nYour gold:  " << gold;
    string mov;
    if (exp > 99)
    {
        cout << "\nRest to level up.";
    }
    else
    {
        cout << "\nYou have " << exp << "/100 exp.";
        string mov;
        cout << "\n"<< name << ", you're at home. Do you want to fight, travel, or steal some bread?  ";
        cin >> mov;
    }
    if (mov == "rest" and exp > 99)
    {
        level_up();
        home_action();
    }
    else if (mov == "fight")
    {
        combat_scene("Guard", 21, 70, 10, 5);
    }
    else if (mov == "stats")
    {
        cout << "\n" << strength << " strength.";
        cout << "\n" << intel << " intelligence.";
        cout << "\n" << sneak << " dexterity.";
        home_action();
    }
    else if (mov == "steal")
    {
        calculate_roll(sneak, 3);
        cout << "\n" << name << ", rolled a "<< die_roll << "!";
        if (goal > die_roll)
        {
            health = max_health;
            magic = max_magic;
            cout << "\n" << name << " stole a loaf of bread and restored health.";
            home_action();
        }
        else
        {
            cout << "\n" << name << " got into a fight!";
            combat_scene("Goblin", 12, 50, 5, 3);
        }
    }
    else if (mov == "quit")
    {
        cout << "\nYou took an arrow to the knee and decided to retire from adventuring.";
        cout << "Thanks for playing!";
    }
    else if (mov == "travel")
    {
        travel_locations();
        cout << "\nWhere do you want to travel to?";
        cin >> current_town;
        location();
    }
    else
    {
        cout << "\nYou cannot "<< mov << ".";
        home_action();
    }
}

This caused the following error:

||=== RPG, Debug ===|
obj\Debug\home_action.o||In function `Z11home_actionv':|
C:\Users\William\Documents\Stuff\Programming\C++\Randomness\RPG\home_action.cpp|64|undefined reference to `travel_locations()'|
C:\Users\William\Documents\Stuff\Programming\C++\Randomness\RPG\home_action.cpp|67|undefined reference to `location()'|
||=== Build finished: 3 errors, 0 warnings (0 minutes, 0 seconds) ===|

The header referenced is (header.h):

#ifndef HEADER_H_INCLUDED
#define HEADER_H_INCLUDED

#include <iostream>

using namespace std;

extern int intel;
extern int strength;
extern int sneak;
extern int damage;
extern int max_health;
extern int max_magic;
extern int enemy_health;
extern int health;
extern int magic;
extern string chosen_class;
extern int die_roll;
extern int goal;
extern int gold;
extern int exp;
extern string name;
extern string current_town;

void level_up();
void calculate_max();
void reset_healths();
void spell();
void attacking_turn();
void home_action();
void calculate_roll(int stat, int difficulty);
void combat_scene(string enemy, int xp_gain, int hp, int dmg, int money);
void location();
int main();
void travel_locations();


#endif // HEADER_H_INCLUDED

And the two functions mentioned in the error are here:

travel_locations():

#include <iostream>
#include "header.h"

using namespace std;

void travel_locations()
{
    if (current_town == "1" || current_town == "murthan")
    {
        cout << "\n2: Worthal";
        cout << "\n3: Einbron";
        cout << "\n4: Home";
    }
    else if (current_town == "2" || current_town == "worthal")
    {
        cout << "\n1: Murthan";
        cout << "\n3: Einbron";
        cout << "\n4: Home";
    }
    else if (current_town == "3" || current_town == "einbron")
    {
        cout << "\n1: Murthan";
        cout << "\n2: Worthal";
        cout << "\n4: Home";
    }
    else if (current_town == "4" || current_town == "home")
    {
        cout << "\n1: Murthan";
        cout << "\n2: Worthal";
        cout << "\n3: Einbron";
    }
}

location():

#include <iostream>
#include "header.h"

using namespace std;

void location()
{
    cout << "\n";
    if (current_town == "1" || current_town == "murthan")
    {
        murthan();
    }
    else if (current_town == "2" || current_town == "worthal")
    {
        worthal();
    }
    else if (current_town == "3" || current_town == "einbron")
    {
        einbron();
    }
    else if (current_town == "4" || current_town == "home")
    {
        cout << "\nYou have returned home.";
        home_action();
    }
    else
    {
        cout << "\nInvalid location. Enter another.";
        cin >> current_town;
    }
}

I cannot figure out what is causing this. Any help would be greatly appreciated.

LemenDrop
  • 160
  • 1
  • 3
  • 11
  • 5
    It's not linking your files properly. This is not a C++ issue, it's an issue with not knowing how to work your IDE. – Crowman Aug 25 '13 at 22:48
  • "Undefined reference" means that your compiler is trying to link the single file it produced (to make an executable out of it). This is wrong. It should first compile all the single files to objects (without linking them), then, it should link all the objects together. – Giulio Franco Aug 25 '13 at 22:49
  • 3
    Having over a dozen `extern ...` in a headerfile doesn't seem right to me. Put stuff in classes! – Mats Petersson Aug 25 '13 at 22:52
  • 1
    possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – chris Aug 25 '13 at 22:55
  • @PaulGriffiths Thanks, I'll work on that. The C++ For Dummies book makes it seem like it'll magically make it work. – LemenDrop Aug 26 '13 at 02:43
  • @MatsPetersson I'm just now learning C++, which is why my code is probably overly long and simple. – LemenDrop Aug 26 '13 at 02:44
  • @Xarathorn, Do yourself a favour and look through our list of [good books](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – chris Aug 26 '13 at 02:50
  • @Xarathorn: you may want to try building your projects by hand for a while, sometimes I think that IDEs are the biggest obstacles to people learning something like C++. "Should compile it all for me" usually translates to "does a bunch of stuff I don't understand and can't fix when it goes wrong", and these issues like separate compilation and linking are really too fundamental to C++ to be just something that happens invisibly in the background when you're trying to learn it. – Crowman Aug 26 '13 at 03:15

0 Answers0