当我遇到这个错误时,我正在编写一个简单的基于文本的 RPG。我使用 Code::Blocks IDE,所以它应该为我编译所有内容。我是 C++ 新手,所以我无法理解所有相当高级的代码都会出现同样的错误。这是它发生在 (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();
}
}
这导致了以下错误:
||=== 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) ===|
引用的标头是(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
错误中提到的两个功能在这里:
旅游位置():
#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";
}
}
地点():
#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;
}
}
我无法弄清楚是什么原因造成的。任何帮助将不胜感激。